`
teamojiao
  • 浏览: 344533 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ActiveMQ+Spring2.5

阅读更多
项目环境:
JDK1.5
ActiveMQ5.2
POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ActiveMQ-Spring</groupId>
  <artifactId>ActiveMQ-Spring</artifactId>
  <packaging>war</packaging>
  <name />
  <version>0.0.1-SNAPSHOT</version>
  <description />
  <build>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
    <resources>
      <resource>
        <directory>${basedir}/src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <webappDirectory>${basedir}/WebRoot</webappDirectory>
          <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5.0-1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>1.2_04</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.faces</groupId>
      <artifactId>jsf-impl</artifactId>
      <version>1.2_04</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
    	<groupId>org.apache.activemq</groupId>
    	<artifactId>activemq-all</artifactId>
    	<version>5.2.0</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>2.5.6</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jms</artifactId>
    	<version>2.5.6</version>
    </dependency>
  </dependencies>
</project>
 
package com.jeff.mq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

/**
* 消息接收者
*
* @author jeff
*/
public class MyReceiver {
        public static void main(String[] args) throws JMSException {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
                JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
                Destination destination = (Destination) ctx.getBean("destination");
                while (true) {
                        TextMessage txtmsg = (TextMessage) template.receive(destination);
                        if (null != txtmsg)
                                System.out.println("收到消息内容为: " + txtmsg.getText());
                        else
                                break;
                }
        }
}
 
package com.jeff.mq;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
* 消息发送者
*
* @author jeff
*/
public class MySender {
        public static void main(String[] args) {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
                JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
                Destination destination = (Destination) ctx.getBean("destination");

                template.send(destination, new MessageCreator() {
                        public Message createMessage(Session session) throws JMSException {
                                return session.createTextMessage("发送消息:Hello ActiveMQ Text Message!");
                        }
                });
                System.out.println("成功发送了一条JMS消息");
        }
}
 
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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 <!-- 配置JMS连接工厂 -->
        <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://localhost:61616"/>
        </bean>

        <!-- 配置JMS模版 -->
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                <property name="connectionFactory" ref="connectionFactory"/>
        </bean>

        <!-- 发送消息的目的地(一个队列) -->
        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
                <!-- 设置消息队列的名字 -->
                <constructor-arg index="0" value="HelloWorldQueue"/>
        </bean> 



</beans>
 
运行发送端三次:
成功发送了一条JMS消息

Process finished with exit code 0
 
然后再运行接收端一次:
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
 
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
分享到:
评论

相关推荐

    ActiveMQ5.1+Spring2.5 Demo

    rt ActiveMQ5.1+Spring2.5的Demo

    Spring+JMS+ActiveMQ+Tomcat实现消息服务_服务器应用

    •Spring 2.5 •ActiveMQ 5.4.0 •Tomcat 6.0.30 下面通过学习与配置,实现消息服务的基本功能:发送与接收。Spring对JMS提供了很好的支持,可以通过JmsTemplate来方便地实现消息服务。这里,我们的消息服务不...

    activemq新手大全

    一、JMS基本概念 二、activemq介绍及安装 1、消息中间件简介 2、activemq ...四、activemq整合spring运用 五、activemq常见问题 5.1 activemq 消息传递 5.2 activemq 消息确认机制 5.3 activemq 持久化机制

    spring-activemq.zip

    spring-boot 2.5 集成 ActiveMQ 发布订阅模式

    dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis:dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis整合

    dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redisdubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis整合#该项目介绍ROOT dubbo管理平台lidong-dubbo-api api模块lidong-dubbo-model model模块lidong-...

    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 创建...

    JAVA上百实例源码以及开源项目

    百度云盘分享 简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对...

    JAVA上百实例源码以及开源项目源代码

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!...

Global site tag (gtag.js) - Google Analytics