ActiveMQspring结合使用

 
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 中文件中配置一直有问题?
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值