ActiveMQspring结合使用

来源:互联网 发布:中文文本挖掘软件 编辑:程序博客网 时间:2024/05/24 22:46
 
1.
package jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
 
//用来接收信息
public class HelloWorldReciverPS {
 
   
   public static void main(String args[]){
      
       ApplicationContext context=new FileSystemXmlApplicationContext(
                new String[]{"src/applicationContext.xml"});
        JmsTemplate jmsTemplate=(JmsTemplate)context.getBean("jmsTemplate");
        Destination destination=(Destination)context.getBean("destination");
      
       while(true){   
         TextMessage msg=(TextMessage)jmsTemplate.receive(destination);
         try {
            System.out.println("Recieved:"+msg.getText());
        } catch (JMSException e) {
            System.out.println(e.getMessage());
        }
       }
           
   }
 
   
     
  
}
package jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
//发送消息
public class HelloWorldSenderPS {
  
    public static void main(String[] args) throws Exception{
        ApplicationContext context = new FileSystemXmlApplicationContext(
                new String[] {"src/applicationContext.xml"});
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");
        //取得目的地,有两种方式:queue和topic
        for (int i = 0; i < 100; i++) {
            System.out.println("Sending Message: Foo"+i);
            jmsTemplate.convertAndSend(destination,"Foo "+i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    }
}
 
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置开源ActiveMQ -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 <property name="brokerURL">
    <value>tcp://localhost:61616</value>
 </property>
 
</bean>
<!-- 采用spring中的jmsTemplate简化jms api-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
   <property name="connectionFactory">
     <ref bean="connectionFactory"/>
   </property>
</bean>
<!-- 发送接收的目的地 -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
 <constructor-arg index="0">
    <value>HelloWorldTopic</value>
 </constructor-arg>
</bean>
</beans>
 
 
注意:测试时要先启动apache-activemq-5.0.0/bin下的activemq.bat
然后分开个项目启动HelloWorldSenderPS 和HelloWorldReciverPS 。
 
也可通过程序来启动activemq:
        //通过程序启动activemq
        BrokerService brokerService=(BrokerService) new BrokerService();
        brokerService.addConnector("tcp://localhost:61616");
        brokerService.start();
写一个Listener就可以了,让应用启动时就启动了。
通过在spring.xml中文件中配置一直有问题?
 
原创粉丝点击