`
haoran_10
  • 浏览: 441168 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

消息中间件(2)-ActiveMq & Spring 技术集成

阅读更多
  • 一、为什么需要spring + activeMq 集成?

1、原生的JMS使用起来,太繁琐,需要封装很多层才能在正式代码中使用,

2、activemq一套开源的JMS实现方案,实现了服务端和客户端,开箱即用

3、spring一整套组件,直接拿来主义

 

  • 二、实现步骤

1、开启activemq服务端

直接从官网下载 http://activemq.apache.org/,开箱即用的东西,略过。

 

2、客户端调用所需要的关键jar包,使用的maven管理,其他包管理类似

 

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jms</artifactId>
	<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-core</artifactId>
	<version>5.7.0</version>
</dependency>
<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-pool</artifactId>
	<version>5.7.0</version>
</dependency>

 

 

实际使用的时候,可以适当调整版本号

 

3、在JMS规范 中,使用JMS几个步骤,

   (1)、用JNDI 得到ConnectionFactory对象;

    (2)、用JNDI 得到目标队列或主题对象,即Destination对象;

    (3)、用ConnectionFactory创建Connection 对象;

    (4)、用Connection对象创建一个或多个JMS Session;

    (5)、用Session 和Destination 创建MessageProducer和MessageConsumer;

    (6)、通知Connection 开始传递消息。

而activemq使用的步骤也是大同小异

 

   3.1、建立ConnectionFactory以及spring高度封装的JmsTemplate

     

@Bean
public PooledConnectionFactory PooledConnectionFactory(){
	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
	connectionFactory.setBrokerURL(MQ_URL);//配置MQ_URL,例如tcp://localhost:61616
	
	PooledConnectionFactory PooledConnectionFactory = new PooledConnectionFactory();
	PooledConnectionFactory.setConnectionFactory(connectionFactory);
	
	return PooledConnectionFactory;
}

@Bean
public JmsTemplate JmsTemplate(){
	JmsTemplate JmsTemplate = new JmsTemplate();
	JmsTemplate.setConnectionFactory(PooledConnectionFactory());
	
	return JmsTemplate;
}

  3.2 建立Queue或者Topic

 

 

@Bean(name="queue1")
public Destination queue1Destination(){
	Destination destination = new ActiveMQQueue(queue1);//队列 定义queue1名称即可
       //Destination destination = new ActiveMQTopic(queue1);//主题
	
	return destination;
}

   

 

   第3,4步直接省略,JmsTemplate已经做好了

   3.3 发送消息

   

@Component
public class MyMessageSender {
	@Autowired JmsTemplate jmsTemplate;
	@Resource(name="queue1") Destination destination;
	
	public void send(String msg){
		jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage message = session.createTextMessage();
				message.setText(msg);
				return message;
			}
		});
	}
}

  发送消息是如此的简单,制定队列名称,就可以直接发送消息到队列或者主题

 

  3.4消息监听,有了消息发送就得有Consumer处理消息

  

@Component
public class MyMessageListener implements MessageListener{

	@Override
	public void onMessage(Message message) {
		System.out.println("==========Thread:"+Thread.currentThread().getId());
		System.out.println("==========MyMessageListener"+message.toString());
	}

}

   以及支持该消息监听的消息容器

    

@Autowired MyMessageListener messageListener;

@Bean(name="queue1ListenerContainer")
	public DefaultMessageListenerContainer DefaultMessageListenerContainer(){
		DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer();
		listenerContainer.setConnectionFactory(PooledConnectionFactory());
		listenerContainer.setDestination(queue1Destination());
		listenerContainer.setMessageListener(messageListener);
		listenerContainer.setConcurrentConsumers(2);//可以理解为起两个线程去消费消息
		return listenerContainer;
	}

 

至此主流程开发结束。

 

 

 

1
1
分享到:
评论

相关推荐

    java消息中间件教程-activemq

    - **1-2 为什么使用消息中间件**:消息中间件作为一种关键的技术组件,在现代软件架构中扮演着重要的角色。它主要用于解决应用程序之间异步通信的问题,提高了系统的解耦合度、可扩展性和容错能力。 #### 二、消息...

    apache-activemq-5.8.0-bin.zip

    - ActiveMQ可以轻松集成到Spring框架中,为Spring应用提供消息传递能力。 - 支持与各种企业级应用服务器如Tomcat、JBoss等集成。 总之,Apache ActiveMQ 5.8.0是一个强大且灵活的消息中间件,通过提供可靠的异步...

    apache-activemq-5.9.0 下载

    10. **Spring框架集成**:ActiveMQ可以与Spring框架无缝集成,简化在Spring应用中使用消息传递的过程。 在使用`apache-activemq-5.9.0`时,用户应根据具体需求配置相应的参数,如存储路径、最大内存、网络监听端口...

    apache-activemq-5.3.0-bin.zip

    - **消息中间件**:ActiveMQ作为消息中间件,负责在分布式系统中传输消息,解耦应用之间的通信。 - **JMS(Java Message Service)**:JMS是Java平台上的标准接口,定义了生产、发送、接收和消费消息的API。 - **...

    Spring-ActiveMQ-jar

    使得开发者能够利用Spring的IoC(Inversion of Control)和AOP(Aspect-Oriented Programming)特性,轻松地在应用中整合Apache ActiveMQ,一个开源的消息中间件。 **Spring框架与ActiveMQ的集成** Spring框架是...

    spring-boot-activemq-demo

    在本示例项目"spring-boot-activemq-demo"中,我们关注的是如何将Spring Boot与Apache ActiveMQ集成,以实现高效的消息传递功能。ActiveMQ是Apache软件基金会的一个开源项目,它是Java消息服务(JMS)的实现,提供了...

    Spring-ActiveMQ.rar_Spring Activemq_activemq_activemq spring

    Spring的`Spring Integration`模块提供了与多种消息中间件集成的API,其中包括ActiveMQ。通过这些API,我们可以定义消息通道、消息转换器以及与ActiveMQ服务器的连接配置。 1. **配置ActiveMQ服务器** 在开始集成...

    apache-activemq-5.12.0.zip

    1. **Apache ActiveMQ介绍**:ActiveMQ是一个完全支持JMS 1.1和1.0.2B规范的消息中间件,提供可靠的消息传递和高效的数据通信。它支持多种协议,包括OpenWire、STOMP、AMQP、XMPP、MQTT等,使得不同的应用程序和平台...

    maven-spring-activemq

    标题 "maven-spring-activemq" 暗示了这是一个关于使用Maven构建Spring项目,并集成ActiveMQ作为消息中间件的实践案例。在这个场景中,我们将探讨如何利用Spring框架与Apache ActiveMQ相结合,实现消息的发布与订阅...

    apache-activemq-5.12.0-bin.tar.gz

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java Message Service(JMS)规范,用于在分布式系统...此外,ActiveMQ还可以与其他技术,如Spring框架、Hadoop等集成,以构建更复杂的分布式系统。

    apache-activemq-5.8.0.zip

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java Message Service(JMS)规范,用于在分布式系统中高效地传输消息。ActiveMQ 5.8.0是该软件的一个版本,提供了稳定和高性能的消息传递服务。...

    apache-activemq-5.13.3

    Apache ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它基于Java Message Service(JMS)规范,用于在分布式系统中实现可靠的消息传递。ActiveMQ在企业级应用中扮演着重要的角色,因为它允许不同应用程序...

    spring-activemq.zip

    而ActiveMQ是Apache组织开发的一款开源消息中间件,它实现了JMS(Java Message Service)规范,允许分布式系统中的组件之间进行异步通信。本项目"spring-activemq.zip"显然关注的是如何在Spring Boot 2.5版本中整合...

    activemq_spring.rar_Spring和ActiveMQ_spring_消息中间件_消息发布订阅_消息订阅

    在IT行业中,消息中间件是实现系统间异步通信、解耦合的关键技术之一,而Apache ActiveMQ就是其中的一款著名开源产品。Spring框架作为Java领域广泛使用的应用框架,提供了与ActiveMQ集成的能力,使得我们可以方便地...

    activemq5.5.1 Spring模板

    总结,ActiveMQ 5.5.1与Spring的集成使得消息处理变得更加简单,通过Spring的`JmsTemplate`,我们可以轻松地构建高效、可靠的分布式系统。理解并熟练掌握这种集成方式,对于提升企业级应用的稳定性和扩展性具有重要...

    Windows版apache-activemq-5.7.0

    - 对于Web应用,可以利用Spring框架集成ActiveMQ,简化消息发送和接收。 10. **故障排查**: - 当遇到问题时,首先检查日志文件(logs/activemq.log)中的错误信息。 - 使用JMX(Java Management Extensions)...

    springboot集成activemq实现消息接收demo

    本教程将详细介绍如何在Spring Boot项目中集成ActiveMQ,实现消息接收的Demo。 首先,我们需要在Spring Boot项目中引入ActiveMQ的相关依赖。在`pom.xml`文件中添加以下Maven依赖: ```xml &lt;groupId&gt;org.spring...

    spring-activeMQ-demo:spring-activeMQ-演示

    Spring框架以其强大的集成能力,使得与各种消息中间件如ActiveMQ的整合变得简单易行。本篇文章将深入探讨如何使用Spring与ActiveMQ进行集成,并通过一个具体的"spring-activeMQ-demo"来展示其实现过程。 首先,让...

    activeMQ_spring简单案例(含XML配置)

    ActiveMQ是Apache软件基金会开发的开源消息中间件,而Spring则是一个广泛使用的Java企业级应用开发框架。将两者结合,我们可以实现高效、可靠的消息通信。 首先,我们需要了解ActiveMQ的基本概念。ActiveMQ作为消息...

    activemq与spring整合源代码

    Spring还提供了丰富的模块,如数据访问、Web、测试等,其中Spring JMS模块专门用于集成消息中间件,使得与ActiveMQ的整合变得简单。 三、ActiveMQ与Spring的整合 1. 添加依赖:首先,在项目中引入ActiveMQ和Spring...

Global site tag (gtag.js) - Google Analytics