【ActiveMQ】开源消息总线ActiveMQ的使用

在阅读的过程中有任何问题,欢迎一起交流

邮箱:1494713801@qq.com   

QQ:1494713801

 

 

1:下载 ActiveMQ 5.6.0 Release 

http://activemq.apache.org/download.html 

放到d盘 

ActiveMQ使用教程

2:运行apache-activemq服务:双击 activemq.bat 

ActiveMQ使用教程 
4:所需jar包 

ActiveMQ使用教程

5:spring配置文件applicationContext.xml 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? xml  version = "1.0"  encoding = "UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
< beans >
     < bean  id = "connectionFactory"  class = "org.apache.activemq.ActiveMQConnectionFactory" >
         < property  name = "brokerURL" >
             < value >tcp://localhost:61616?wireFormat.maxInactivityDuration=0</ value >
         </ property >
     </ bean >
     < 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.ActiveMQQueue" >
         < constructor-arg  index = "0" >
             < value >MessageQueue</ value >
         </ constructor-arg >
     </ bean >
</ beans >

这时主要是配置activemq服务信息与实现springjms的对应接口 

6:消息产生者 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package  test;
import  javax.jms.JMSException;
import  javax.jms.Message;
import  javax.jms.Session;
  
import  org.springframework.jms.core.MessageCreator;
  
/**
  * 消息产生者
  * User: liuwentao
  * Time: 12-6-14 上午11:31
  */
public  class  MyMessageCreator  implements  MessageCreator {
     public  int  n =  0 ;
     private  static  String str1 =  "这个是第 " ;
     private  static  String str2 =  " 个测试消息!" ;
     private  String str =  "" ;
     @Override
     public  Message createMessage(Session paramSession)  throws  JMSException {
         System.out.println( "MyMessageCreator  n="  + n);
         if  (n ==  9 ) {
             //在这个例子中表示第9次调用时,发送结束消息
             return  paramSession.createTextMessage( "end" );
         }
         str = str1 + n + str2;
         return  paramSession.createTextMessage(str);
     }
}

7:发送消息方 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package  test;
import  javax.jms.Destination;
  
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  org.springframework.jms.core.JmsTemplate;
/**
  * 发送消息方
  * User: liuwentao
  * Time: 12-6-14 上午11:29
  */
public  class  MessageSender  extends  Thread {
     public  static  void  main(String args[])  throws  Exception {
         String[] configLocations =  new  String[] { "test/applicationContext.xml" };
         ApplicationContext context =  new  ClassPathXmlApplicationContext(configLocations);
         JmsTemplate jmsTemplate = (JmsTemplate) context.getBean( "jmsTemplate" );
         Destination destination = (Destination) context.getBean( "destination" );
         for  ( int  i =  1 ; i <  100 ; i++) {
             System.out.println( "发送 i="  + i);
             //消息产生者
             MyMessageCreator myMessageCreator =  new  MyMessageCreator();
             myMessageCreator.n = i;
             jmsTemplate.send(destination, myMessageCreator);
             sleep( 10000 ); //10秒后发送下一条消息
         }
     }
}

8:消息接收方 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package  test;
import  javax.jms.Destination;
import  javax.jms.TextMessage;
  
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  org.springframework.jms.core.JmsTemplate;
/**
  * 消息接收方
  * User: liuwentao
  * Time: 12-6-14 上午11:32
  */
public  class  MessageReciver{
     public  static  void  main(String args[])  throws  Exception {
         String[] configLocations =  new  String[] { "test/applicationContext.xml" };
         ApplicationContext context =  new  ClassPathXmlApplicationContext(configLocations);
  
         JmsTemplate jmsTemplate = (JmsTemplate) context.getBean( "jmsTemplate" );
         Destination destination = (Destination) context.getBean( "destination" );
  
         TextMessage msg =  null ;
         //是否继续接收消息
         boolean  isContinue =  true ;
         while  (isContinue) {
             msg = (TextMessage) jmsTemplate.receive(destination);
             System.out.println( "收到消息 :"  + msg.getText());
             if  (msg.getText().equals( "end" )) {
                 isContinue =  false ;
                 System.out.println( "收到退出消息,程序要退出!" );
             }
         }
         System.out.println( "程序退出了!" );
     }
}

9:测试 

运行 发送方和接收方 (顺序随便) main文件,效果如下: 

  

注:即使 接收方启动晚,或者 发送方关闭了, 接收方都会正常接收完所有数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值