您好,登錄后才能下訂單哦!
實現消息隊列的兩種方式
Apache ActiveMQ官方實例發送消息
直接在Apache官網http://activemq.apache.org/download-archives.html下載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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。