`
coach
  • 浏览: 383009 次
  • 性别: Icon_minigender_2
  • 来自: 印度
社区版块
存档分类
最新评论

通过spring开发ActiveMQ简单应用

阅读更多
使用spring的支持类开发JMS程序可以简化代码,确保开发质量。以下是使用ActiveMQ作为JMS实现的示例。

首先需要确保如下类库,这里使用maven的pom:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>2.5.6</version>
</dependency>
<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.2.0</version>
</dependency>


编写消息生产者的代码:

@Component
public class MyMessageProducer {
    @Resource(name = "Queue")
    private Destination destination;

    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(final String message) {
        this.jmsTemplate.send(this.destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText(message);
                return textMessage;
            }
        });
    }
}

有关生产者部分spring的配置文件内容:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
     http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <context:annotation-config />
    <context:component-scan base-package="activemq.demo.spring" />

    <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://localhost:61616</value>
        </property>
    </bean>

    <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory" />
    </bean>
    <amq:queue name="Queue" physicalName="example.D" />

这样,执行发送代码,在activemq的admin界面中可以观察到发送成功的消息。

异步接收的代码:

public class MyMessageConsumer implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(">>>>>>>"+textMessage.getText());
//            throw new RuntimeException(">>>>>>>>>>test error");
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }

}

有关异步接收代码的spring配置:

     <bean id="myMessageConsumer" class="activemq.demo.spring.MyMessageConsumer" />
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="destination" ref="Queue" />
        <property name="messageListener" ref="myMessageConsumer" />
        <property name="sessionTransacted" value="true"/>
    </bean>


这样,就实现了基于spring和activemq的JMS的同步发送和异步提交。如果将MyMessageConsumer的抛出异常部分取消注释,则接收信息后,spring捕获到该异常并自动回滚事务。通过activemq的admin界面会监控到消息依然留在队列中。
分享到:
评论

相关推荐

    JAVA编程之Spring-activeMQ基础开发

    # Spring-activeMQ 在业务逻辑的异步处理,系统解耦,分布式通信以及控制高并发的场景下,消息队列有着广泛的应用。本项目基于Spring这一平台,整合流行的开源消息队列中间件ActiveMQ,实现一个向ActiveMQ添加和读取...

    Spring ActiveMQ安装、配置、打包服务及实例

    ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。 附件资料主要含:ActiveMQ包,安装配置文档,将服务端...

    Spring平台整合消息队列ActiveMQ实现发布订阅、生产者消费者模型(适合新手或者开发人员了解学习ActiveMQ机制)

    本项目基于Spring这一平台,整合流行的开源消息队列中间件ActiveMQ,实现一个向ActiveMQ添加和读取消息的功能。并比较了两种模式:生产者-消费者模式和发布-订阅模式的区别。 包含的特性如下: 1.开启activeMQ,访问...

    MQ之ActiveMQ.mmap

    从零基础入门到熟练掌握ActiveMQ,能够结合Spring/SpringBoot进行实际开发配置并能够 进行MQ多节点集群的部署,最后学习MQ的高级特性和高频面试题的分析。 希望通过本次的学习, 能够帮助同学们取得更大的进步,加油...

    消息队列activemq的实际应用

    这是我学习消息队列的总结而编写的activemq的使用代码,使用SpringBoot集成activemq以及简化使用代码,让初学者更容易接受,同时让高级开发看起来更加舒服

    实战ActiveMQ集群与应用视频教程.zip

    网盘文件永久链接 1:ActiveMQ入门和消息中间件 ...6:ActiveMQ结合Spring开发 7:ActiveMQ支持的传输协议 8:ActiveMQ消息存储持久化 9:ActiveMQ的静态网络链接 10:多线程consumer访问集群 ..........

    消息中间件之ActiveMQ视频课程

    本套视频以Apache的ActiveMQ作为切入点,分为基础/实战/面试上中下三大部分,带你从零基础入门到熟练掌握ActiveMQ,能够结合Spring/SpringBoot进行实际开发配置并能够进行MQ多节点集群的部署,可以学习到MQ的高级...

    Spring Boot应用集成打包部署.docx

    Spring Boot是一个开发框架,目的是简化Spring应用的初始搭建过程和开发过程,它提供了默认的代码和注解配置,简化了开发过程,它可以与MyBaties、Redis、ActiveMQ等多种技术进行集成使用,简化了项目开发的过程

    ActiveMQ.rar

    包括:多种启动Broker的方法、单独应用的开发、结合Spring的开发等 n 五:ActiveMQ的Transport 包括:多种传输协议的功能、配置和使用 六: ActiveMQ的消息存储 包括:队列和topic、KahaDB、AMQ、JDBC、MMS等 n 七:...

    spring boot 实践学习案例,与其它组件整合

    - Spring Boot 集成 WebFlux 开发反应式 Web 应用 - springboot-dubbo - Spring Boot 集成 Dubbo 的三种方式 - springboot-search - Spring Boot 集成 搜索引擎,包括 elasticsearch、solr - springboot-mq ...

    springCloud

    Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种...

    Spring in Action(第2版)中文版

    10.1.3在spring中安装activemq 10.2协同使用jms和spring 10.2.1处理冗长失控的jms代码 10.2.2使用jms模板 10.2.3转换消息 10.2.4将spring的网关支持类应用于jms 10.3创建消息驱动pojo 10.3.1创建消息监听器 ...

    Spring in Action(第二版 中文高清版).part2

    10.1.3 在Spring中安装ActiveMQ 10.2 协同使用JMS和Spring 10.2.1 处理冗长失控的JMS代码 10.2.2 使用JMS模板 10.2.3 转换消息 10.2.4 将Spring的网关支持类应用于JMS 10.3 创建消息驱动POJO 10.3.1 创建...

    Spring in Action(第二版 中文高清版).part1

    10.1.3 在Spring中安装ActiveMQ 10.2 协同使用JMS和Spring 10.2.1 处理冗长失控的JMS代码 10.2.2 使用JMS模板 10.2.3 转换消息 10.2.4 将Spring的网关支持类应用于JMS 10.3 创建消息驱动POJO 10.3.1 创建...

    JavaEE求职简历-姓名-JAVA开发工程师.docx

    2.熟悉Java Web应用开发,熟悉listener、EL、JSTL、Filter等常用技术; 3.熟悉Spring MVC、Spring Boot、Spring Data、Spring Security的使用,了解Spring的Ioc与Aop思想; 4.拥有基于Spring整合常用框架并进行项目...

    SpringSecurity安全框架企业中应用

    本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中最常用的高并发解决方案技术, 如 dubbo,redis,solr,freemarker,activeMQ,springBoot框架,微信支付,nginx负载均衡,电商活动秒杀,spring...

    基于Java的智能客服系统设计与实现

    本系统主要是做基于 Java 的一个智能客服系统,使用 Spring 框架,MySQL 数据库、ActiveMq 消息中间件、Redis 缓存、Elasticsearch 全文搜索等服务,一个智能客服系统包含的开发任务有:主要包括前端的页面开发,...

    Spring中注解式事务管理在项目中应用

    本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中最常用的高并发解决方案技术, 如 dubbo,redis,solr,freemarker,activeMQ,springBoot框架,微信支付,nginx负载均衡,电商活动秒杀,spring...

    SpringCloud+vue+uniapp智慧物联/物业/巡检/停车管理系统源码

    基于SpringCloud微服务、分布式架构,更易扩展;项目前后端分离,后端使用JAVA,前端VUE,Uni-app框架;MySQL、Redis多种数据存储方式,只为更快;ActiveMq订阅消息队列,让订单更快流转。 二、项目应用多端 管理...

Global site tag (gtag.js) - Google Analytics