服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现消息队列的两种方式(小结)

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

2021-06-21 13:54朽木要自雕 Java教程

本文主要介绍了两种java实现消息队列的方式,利用Spring消息模板发送消息和Apache ActiveMQ官方实例发送消息,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

实现消息队列的两种方式

apache activemq官方实例发送消息

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

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

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

然后倒入ide

如下:

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

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

启动服务:

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

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

利用spring消息模板发送消息

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

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

生成者代码:

?
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://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemalocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.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.command.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);
 }
}

测试结果

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

代码地址

https://github.com/wahnn/springjms
https://gitee.com/wahnn/springjms

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

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

延伸 · 阅读

精彩推荐