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

网友投稿 468 2023-01-17


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

实现消息队列的两种方式

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配置代码

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

">

<!--启用注解-->

&ltWgakjEMgjY;constructor-arg>

queue1

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

">

<!--启用注解-->

&ltWgakjEMgjY;constructor-arg>

queue1

测试代码

package com.jsm.test;

import com.jms.service.CustomerService;

import com.jms.service.ProducWgakjEMgjYerService;

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:数字化协同研发管理平台(数字化协同研发管理平台有哪些)
下一篇:在Windows系统下安装Thrift的方法与使用讲解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~