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

activemq初步实践-生产者

阅读更多

最近学习activemq与spring的简单整合,通过连个web项目来模拟消息的发送以及接受机制,这里只是讲了如何在两个应用之间进行简单的通信,至于名词解释网上太多博客都有。第一次写博客,比较low,忘见谅哈!本文参考了
http://blog.csdn.net/jiuqiyuliang/article/details/48758203
工程采用maven构建,结构如下

 

其中lion-war为消息发送端,lion-consumer为消息接受端

第一步:官网下载activemqhttp://activemq.apache.org/

第二步:spring整合activemq

一:消息生产者lion-war配置

(1)添加核心依赖:

 

<!--消息机制-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>${activemq.version}</version>
</dependency>

(2)添加配置文件spring-jms.xml

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
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
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd">
    <!--connectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>
    <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!--生产者 start-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <!--队列模式-->
<property name="pubSubDomain" value="false"/>
    </bean>
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <!--主题模式-->
<property name="pubSubDomain" value="true"/>
    </bean>

</beans>

 (3)在web.xml初始化时加上

 (4)由于使用的springmvc,所以在此写个controller,JmsController.java


package com.lion.controller.mq;

import com.lion.service.mq.QueueProduceService;
import com.lion.service.mq.TopicProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by lucoo on 2016/10/18.
 */
@Controller
public class JmsController {
    @Autowired
private QueueProduceService queueProduceService;
    @Autowired
private TopicProduceService topicProduceService;

    /**
     * 队列测试
     * @return
*/
@RequestMapping(value = "/sendQueue")
    public String sendQueue() {
        queueProduceService.send("queueDestination", "hello world!i am from queue");
        return "/index";
    }

    /**
     * 发布订阅测试
     * @return
*/
@RequestMapping(value = "/sendTopic")
    public String publishTopic() {
        topicProduceService.publishTopic("topicDestination", "hello world!i am from topic");
        return "/index";
    }
}

 (5)service层写个两个测试service:QueueProduceService.java 和TopicProduceService.java

package com.lion.service.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * Created by lucoo on 2016/10/17.
 * 队列消息发送
 */
@Service
public class QueueProduceService {
    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    /**
     *
     * @param queueName(和监听器的保持一致)
     * @param message
*/
public void send(String queueName, String message) {
        jmsTemplate.send(queueName, new MessageCreator() {
            @Override
public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

package com.lion.service.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by lucoo on 2016/10/18.
 * 主题消息发送
 */
@Service
public class TopicProduceService {
    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate jmsTemplate;

    public void publishTopic(String topic, String message) {
        jmsTemplate.send(topic, new MessageCreator() {
            @Override
public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

到这里发送端的配置结束
消费者配置:http://lucoo.iteye.com/blog/2331440
  • 大小: 63.1 KB
  • 大小: 215.2 KB
分享到:
评论

相关推荐

    activemq-protobuf-1.1-API文档-中文版.zip

    赠送jar包:activemq-protobuf-1.1.jar; 赠送原API文档:activemq-protobuf-1.1-javadoc.jar; 赠送源代码:activemq-protobuf-1.1-sources.jar; 包含翻译后的API文档:activemq-protobuf-1.1-javadoc-API文档-...

    activemq-cpp-library-3.9.5 编译的windows库文件,支持vs2015、vs2017

    《ActiveMQ-CPP Library 3.9.5在Windows环境下的编译与应用》 ActiveMQ-CPP Library 3.9.5是一款专为C++开发者设计的、用于与Apache ActiveMQ集成的库,它提供了丰富的API接口,使得在C++环境中能够方便地发送和...

    activemq-cpp-library-3.9.5-src.zip

    - **生产者与消息**:生产者对象负责创建和发送消息。消息可以是文本、二进制或者其他类型的数据,且支持各种消息头和属性。 - **事务支持**:ActiveMQ-CPP支持基于会话的事务,确保消息的一致性和可靠性。 - ** ...

    apache-activemq-5.9.0-bin

    这个“apache-activemq-5.9.0-bin”压缩包包含了Apache ActiveMQ 5.9.0版本的完整二进制文件,用于在本地或网络环境中安装和运行。 Apache ActiveMQ的核心功能包括: 1. **消息队列**:ActiveMQ支持多种消息模式,...

    apache-activemq-5.15.8-bin.zip

    这个"apache-activemq-5.15.8-bin.zip"文件包含了ActiveMQ的可执行版本,用于在本地计算机上安装和运行ActiveMQ服务。 首先,我们需要了解ActiveMQ的核心概念。它是一个消息代理,扮演着消息生产者与消费者之间的...

    activemq-all-5.2.0.jar包

    activemq-all-5.2.0.JAR包,欢迎下载。编写java中间件的时候会用到。这是activemq实现的jms中间件。希望能帮助到你。

    activemq-web-console-5.11.2

    activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的。其实activemq-web-console完全可以和activemq-broker分开来部署。 activemq-web-console包含3个apps, 1.一...

    apache-activemq-5.8.0-bin.zip

    - ActiveMQ作为JMS实现,提供了生产者、消费者模型,使得消息生产者可以在不关心消息消费者状态的情况下发送消息,提高了系统的解耦性。 3. **文件结构解析** - `META-INF`目录:包含关于此发布版的元数据,如...

    activemq-pool-5.8.0-sources.jar

    activemq-pool-5.8.0-sources.jar

    activemq-protobuf-1.1.jar

    activemq-protobuf-1.1.jar;activemq-protobuf-1.1.jar

    activemq-core-5.7.0-API文档-中英对照版.zip

    赠送jar包:activemq-core-5.7.0.jar; 赠送原API文档:activemq-core-5.7.0-javadoc.jar; 赠送源代码:activemq-core-5.7.0-sources.jar; 包含翻译后的API文档:activemq-core-5.7.0-javadoc-API文档-中文...

    apache-activemq-5.13.2-bin.tar.gz

    这种通信方式提高了系统的可扩展性和可靠性,因为消息可以在生产者和消费者之间独立传输,即使它们在不同的时间运行或位于不同的网络上。 部署ActiveMQ在Linux环境下,首先需要确保系统已经安装了Java Development ...

    apache-activemq-5.14.3-bin.zip

    这个"apache-activemq-5.14.3-bin.zip"压缩包包含了在Windows环境下部署和运行ActiveMQ所需的所有文件。让我们深入探讨一下这个版本的ActiveMQ及其在Java消息服务中的应用。 首先,Java消息服务(JMS)是一种标准...

    activemq-all-5.8.0.jar

    activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载

    activemq-all-5.6.0.jar

    activemq-all-5.6.0.jar activemq-all-5.6.0.jar activemq-all-5.6.0.jar activemq-all-5.6.0.jar

    activemq-web-4.0-M3.jar.zip

    在提供的压缩包"activemq-web-4.0-M3.jar.zip"中,有两个主要文件:"activemq-web-4.0-M3.jar"和"license.txt"。"activemq-web-4.0-M3.jar"是核心的Java档案文件,包含了运行ActiveMQ Web UI所需的所有类和资源。这...

    apache-activemq-5.5.1-bin.zip加上入门demo

    解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。 包含了apache-activemq-5.5.1-bin.zip以及ActiveMQ一个helloworld的demo启动ActiveMQ以后,登陆:...

    activemq-all-5.2.0-sources.jar

    activemq-all-5.2.0-sources.jar

    activemq-all-5.2.0-jar包

    使用`activemq-all-5.2.0.jar`,开发者可以通过JMS API来创建连接、生产者、消费者和消息。此外,ActiveMQ还提供了XML配置文件,用于设置服务器、网络连接、安全策略等。 **部署和运行** 在Java环境中,将`activemq...

    activemq-broker-5.9.1.jar

    activemq-broker-5.9.1.jar,activemq-broker-5.9.1.jar,activemq-broker-5.9.1.jar

Global site tag (gtag.js) - Google Analytics