好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现消息队列的两种方式(小结)

实现 消息队列 的两种方式

apache activemq官方实例发送消息

直接在apache官网 http://activemq.apache.org/download-archives.html 下载ActiveMQ源码 

下载解压后拿到 java 代码实例

然后倒入ide

如下:

请认真阅读readme.md文件,大致意思就是把项目打成两个jar包,然后启动服务,然后同时运行打的两个jar包,然后就能看到具体的调用信息。打jar包时直接利用maven打就行了,不用修改代码。

启动服务:

利用spring消息模板发送消息

spirng对apache activemq提供了很好的支持

生成者代码:

?

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 com.jms.service.impl;

 

import com.jms.service.producerservice;

import org.springframework.jms.core.jmstemplate;

import org.springframework.stereotype.service;

 

import javax.annotation.resource;

import javax.jms.destination;

 

/**

  * 发送消息

  */

@service

public class producerserviceimpl implements producerservice {

 

  @resource

  private jmstemplate jmstemplate;

 

  public void sendmessage(destination destination, string msg) {

   system.out.println( "向队列" +destination+ "发送消息" );

   jmstemplate.convertandsend(destination,msg);

  }

 

  public void sendmessage(string msg) {

   system.out.println( "向队列" +jmstemplate.getdefaultdestination().tostring()+ "发送消息" );

   jmstemplate.convertandsend(msg);

  }

}

消费者代码:

?

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

package com.jms.service.impl;

 

import com.jms.service.customerservice;

import org.springframework.jms.core.jmstemplate;

import org.springframework.stereotype.service;

 

import javax.annotation.resource;

import javax.jms.destination;

import javax.jms.jmsexception;

import javax.jms.textmessage;

 

@service

public class customerserviceimpl implements customerservice {

 

  @resource

  private jmstemplate jmstemplate;

  /**

   * 接收消息

   * @param destination

   */

  public void receive(destination destination) {

 

   textmessage textmessage = (textmessage) jmstemplate.receive(destination);

   try {

    system.out.println( "从队列》" +destination.tostring()+ "成功获取消息》" +textmessage.gettext());

   } catch (jmsexception e) {

    e.printstacktrace();

   }

 

  }

}

spring配置代码

?

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

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

   xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

   xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

   xmlns:mvc= "http://HdhCmsTestspringframework.org/schema/mvc"

   xsi:schemalocation="http: //HdhCmsTestspringframework.org/schema/beans

   http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

   http: //HdhCmsTestspringframework.org/schema/context

   http: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd

   http: //HdhCmsTestspringframework.org/schema/mvc

   http: //HdhCmsTestspringframework.org/schema/mvc/spring-mvc.xsd

   ">

 

  <!-- 启动包扫描功能,以便注册带有 @controller 、 @service 、 @repository 、 @component 等注解的类成为spring的bean -->

  <context:component-scan base- package = "com.jms.service" > </context:component-scan>

 

  <!-- 配置根视图 -->

  <!--<mvc:view-controller path= "/" view-name= "index" />-->

 

  <!--启用注解-->

  <mvc:annotation-driven />

 

  <!-- 视图层配置 -->

  <!--<bean class = "org.springframework.web.servlet.view.internalresourceviewresolver" >-->

   <!--<property name= "prefix" value= "/web-inf/view/" />-->

   <!--<property name= "suffix" value= ".jsp" />-->

  <!--</bean>-->

 

 

  <!-- 配置jms连接工厂 -->

  <bean id= "connectionfactory" class = "org.apache.activemq.activemqconnectionfactory" >

   <property name= "brokerurl" value= "tcp://localhost:61616" />

  </bean>

 

  <!-- 定义消息队列(queue) -->

  <bean id= "queuedestination" class = "org.apache.activemq测试数据mand.activemqqueue" >

   <!-- 设置消息队列的名字 -->

   <constructor-arg>

    <value>queue1</value>

   </constructor-arg>

  </bean>

 

  <!-- 配置jms模板(queue),spring提供的jms工具类,它发送、接收消息。 -->

  <bean id= "jmstemplate" class = "org.springframework.jms.core.jmstemplate" >

   <property name= "connectionfactory" ref= "connectionfactory" />

   <property name= "defaultdestination" ref= "queuedestination" />

   <property name= "receivetimeout" value= "10000" />

  </bean>

</beans>

测试代码

?

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 com.jsm.test;

 

import com.jms.service.customerservice;

import com.jms.service.producerservice;

import org.junit.test;

import org.springframework.context.support.classpathxmlapplicationcontext;

 

import javax.jms.destination;

 

/**

  * 消息队列测试类

  */

public class jmstest {

 

 

  @test

  public void producertest(){

 

   classpathxmlapplicationcontext springcontext = new classpathxmlapplicationcontext( new string[]{ "classpath:spring-core.xml" });

   producerservice producerservice = (producerservice)springcontext.getbean( "producerserviceimpl" );

   customerservice customerservice = (customerservice)springcontext.getbean( "customerserviceimpl" );

 

   destination destination = (destination)springcontext.getbean( "queuedestination" );

   producerservice.sendmessage( "测试消息队列" );

   customerservice.receive(destination);

  }

}

测试结果

代码地址

https://github测试数据/wahnn/springjms
https://gitee测试数据/wahnn/springjms

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/fenfenguai/article/details/79257928

查看更多关于java实现消息队列的两种方式(小结)的详细内容...

  阅读:12次