java实现消息队列的两种方式(小结)
程序员文章站
2024-03-04 10:28:47
实现消息队列的两种方式
apache activemq官方实例发送消息
直接在apache官网下载activemq源码
下载解压后拿到java代码实例...
实现消息队列的两种方式
apache activemq官方实例发送消息
直接在apache官网下载activemq源码
下载解压后拿到java代码实例
然后倒入ide
如下:
请认真阅读readme.md文件,大致意思就是把项目打成两个jar包,然后启动服务,然后同时运行打的两个jar包,然后就能看到具体的调用信息。打jar包时直接利用maven打就行了,不用修改代码。
启动服务:
利用spring消息模板发送消息
spirng对apache activemq提供了很好的支持
生成者代码:
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); } }
消费者代码:
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配置代码
<?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>
测试代码
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.com/wahnn/springjms
https://gitee.com/wahnn/springjms
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。