spring-jms(activemq实现)使用queue发送消息简单例子

activemq实现了jms规范,使用jms发送消息,从消息的一端发送到另一端,当加入了activemq作为中间作,我们可以使用异步的方式发送消息.大概过程:发送者与activemq建立连接,先发送消息到activemq.然后,接收者与activemq建立连接,从activemq取回消息.发送者发消息的时候不依赖接收者,接收者接收消息的时候不依赖发送者,两个过程可以不同步进行.达到应用解耦的作用.如果接收者不去取消息,消息也没过期,那么消息会一直驻留在activemq这个中间件.
下面只是一个简单例子.接收者和发送者,几乎同时与activemq建立连接,接收者建立起监听,发送者一发送消息,接收者就马上接收到消息.


一.注册Connection Factory Bean.

    @Bean(destroyMethod = "stop")
    public PooledConnectionFactory pooledConnectionFactory(){
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616");
        return new PooledConnectionFactory(activeMQConnectionFactory);
    }

二.注册Destination Bean(使用队列发送消息).

    @Bean
    public ActiveMQQueue activeMQQueue(){
        return new ActiveMQQueue("ExamQueueName");
    }

三.创建Consumer.
  1.注册MessageListener实现Bean.
    @Bean
    public ExamQueueListener examQueueListener(){
        return new ExamQueueListener();
    }

其中ExamQueueListener.java如下:
package com.exam.jms;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created by nil on 2014/8/26.
 */
public class ExamQueueListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println(message);
    }
}


  2.使用connection,destination和listener创建consumer.

    @Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer(){
        DefaultMessageListenerContainer defaultMessageListenerContainer=new DefaultMessageListenerContainer();
        defaultMessageListenerContainer.setConnectionFactory(pooledConnectionFactory());
        defaultMessageListenerContainer.setDestination(activeMQQueue());
        defaultMessageListenerContainer.setMessageListener(examQueueListener());
        return defaultMessageListenerContainer;
    }

四.创建Producer.
  1.使用spring的JmsTemplate.

    @Bean
    public JmsTemplate jmsTemplate(){
        return new JmsTemplate(pooledConnectionFactory());
    }

  2.将destination和jmsTemplate注入到examPublisher来注册Producer Bean.

    @Bean
    public ExamPublisher examPublisher(){
        return new ExamPublisher(jmsTemplate(),activeMQQueue());
    }

 其中,ExamPublisher.java内容如下:
package com.exam.jms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import java.util.Random;
/**
 * Created by nil on 2014/8/26.
 */
public class ExamPublisher {
    private JmsTemplate jmsTemplate;
    private Destination destination;
    public ExamPublisher(){}
    public ExamPublisher(JmsTemplate jmsTemplate,Destination destination){
        this.jmsTemplate = jmsTemplate;
        this.destination = destination;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    public void setDestination(Destination destination) {
        this.destination = destination;
    }
    public void sendMessage(final String msg){
        jmsTemplate.send(destination,new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("test message " + msg);
            }
        });
    }
}

五.单元测试(测试前不要忘了先启动activemq).ExamPublisherTest.java内容如下:

package com.exam.jms;
import com.exam.config.AppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
/**
 * Created by nil on 2014/8/26.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,classes={AppConfig.class})
public class ExamPublisherTest {
    @Resource
    private ExamPublisher examPublisher;
    @Test
    public void testSendMessage(){
        examPublisher.sendMessage("x123x");
    }
}

六.应用到Web
1.JmsController.java内容如下:
package com.exam.web;
import com.exam.jms.ExamPublisher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
public class JmsController {
    @Resource
    private ExamPublisher examPublisher;
    @RequestMapping("/test")
    public String test(String message){
        examPublisher.sendMessage(message);
        return "redirect:/index.html";
    }
}
2.index.html内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test.do">
    <input type="text" name="message" value="test"/>
    <input type="submit" value="submit"/>
</form>
</body>
</html>
3.启动jetty,在浏览器打开index.html页面测试(测试前不要忘了先启动activemq).


可能遇到的问题.当停止jetty或tomcat之后,activemq会报下面的警告.警告的原因估计是:客户端(包括Producer和Consumer)没有正确断开连接,上面的例子就是从PooledConnectionFactory生成多少个活跃的连接,就会有多少个警告,(PooledConnectionFactory的默认最大连接数是1,当然注册Bean时可以改).可能是直接断了进程,没有调用stop方法,从输出的警告,我认为问题不大.

WARN | Transport Connection to: tcp://127.0.0.1:65279 failed: java.net.SocketException: Connection reset


源码:http://download.csdn.net/detail/xiejx618/7822573

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMS工厂(JMS Connection Factory)是JMS规范中的概念,它是用来创建JMS Connection(即JMS连接)的工厂类。在使用JMS时,我们需要先创建一个JMS连接,然后使用这个连接来创建JMS会话(JMS Session),最后使用JMS会话来发送和接收JMS消息。 针对不同的JMS消息中间件,我们需要使用不同的JMS Connection Factory。在使用ActiveMQ时,我们可以使用ActiveMQ提供的ActiveMQConnectionFactory来创建JMS连接。 而在Spring JMS框架中,我们可以使用Spring提供的ConnectionFactory来创建JMS Connection Factory。Spring提供了多个ConnectionFactory的实现类,例如: - ActiveMQConnectionFactory:用于创建ActiveMQJMS Connection Factory。 - CachingConnectionFactory:用于创建基于缓存的JMS Connection Factory。 - SingleConnectionFactory:用于创建单例的JMS Connection Factory。 在Spring Boot中,我们可以通过配置文件来配置JMS Connection Factory,例如: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.jms.pub-sub-domain=false spring.jms.template.default-destination=example.queue ``` 在上面的配置中,我们指定了ActiveMQ的地址和端口号,以及默认的JMS目的地(即消息队列或主题)。 总之,JMS Connection Factory是JMS规范中的概念,用于创建JMS连接。在使用ActiveMQ时,我们可以使用ActiveMQ提供的ActiveMQConnectionFactory来创建JMS Connection Factory。在Spring JMS框架中,我们可以使用Spring提供的ConnectionFactory来创建JMS Connection Factory,也可以使用其他第三方提供的JMS Connection Factory。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值