java实现消息队列的两种方式(小结)
作者:朽木要自雕 发布时间:2022-07-09 08:12:13
标签: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配置代码
<?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
来源:https://blog.csdn.net/fenfenguai/article/details/79257928
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 阅读提示 具有mybatis基础,熟练使用mybatis-plus。概述 我们都知道,mybatis-plus是一个mybatis的增强
- 序章简介:bean的加载控制指根据特定情况对bean进行选择性加载以达到适用项目的目标。根据之前对bean加载的八种方式,其中后面四种是可以
- 一个框架的使用,必然离不开其中的组件支持。我们在下载完mybatis框架后,因为大部分的内部结构还没有启动,就要手动的对其进行配置。在之前有
- 故障:收到服务器报警,内存使用率超过80%1.查看使用dstat和top查看内存使用最高的应用使用dstat查到内存占用最高的是java应用
- 最近在学ssh,一直搞不懂$,%,#的区别,做了点小练习,慢慢也懂了一点,将自己所学的也记录下来吧。 存在一下一个实
- 1 配置文件的方法我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量
- java ,javaw 和 javaws 的区别:首先,所有的这些都是java的启动装置,java.e
- 概念二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:1、若它的左子树不为空,则左子树上所有节点的值都小于根结点的值。
- 1. Limit实现分页1.1 为什么需要分页 减少数据的处理量1.2 使用Limit实现分页select * from user limi
- 1.pom.xml<?xml version="1.0" encoding="UTF-8"?&
- 本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。App
- 我就废话不多说了,大家还是直接看代码吧~ public static void main(String[] args) { &n
- 一、NIO基本简介NIO (New lO)也有人称之为java non-blocking lO是从Java 1.4版本开始引入的一个新的IO
- Java操作Redis的方式有下面两种:一、jedis(1)maven配置<dependency> <grou
- 1、static是什么意思?static 关键字表明一个成员变量或者是成员方法可以在没有所属的类的实例变量的情况下被访问。例如Main类pa
- 1.什么是mybatis动态sql看到动态,我们就应该想到,这是一个可以变化的sql语句MyBatis的动态SQL是基于OGNL表达式的,它
- 在Windows平台上,播放PCM声音使用的API通常有如下两种。waveOut and waveIn:传统的音频MMEAPI,也是使用的最
- 一、访问或添加request/session/application属性public String scope() throws Excep
- 本章是后续学习的基石,只有充分理解了分布式系统的概念和面临的问题,以及ZooKeeper内部的概念,才能懂得ZooKeeper是如何对分布式
- BigDecimal的舍入模式(RoundingMode)BigDecimal.divide方法中必须设置roundingMode,不然会报