maven构建springmvc+activeMQ

一、简介

关于jms,请参阅https://docs.oracle.com/javaee/6/tutorial/doc/bncdr.html,看内容是做rpc服务的东西,公司用的是weblogic自带的jms,其实原理差不多,都是创建一个消息工厂,添加队列,从队列中发消息;现在应该不少公司再用dubbo和zookeeper做吧,这个以后再研究。

二、使用

1.添加依赖

       
        
            org.apache.activemq
            activemq-core
            5.7.0
        
        
            org.apache.activemq
            activemq-pool
            5.12.1
        

        
        
            org.apache.xbean
            xbean-spring
            3.16
        

2.生产消费者模式,其实就是发送者和接收者

package com.web.jms.service;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;

@Service
public class ProducerService {

    @Resource(name = "jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    @Resource(name = "queueDestination")
    private Destination responseDestination;

    public void sendMessage(final String msg){
        System.out.println(">>>>\n"+Thread.currentThread().getName()+" 向队列"+responseDestination+"发送消息---------------------->"+msg);
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(msg);
                textMessage.setJMSReplyTo(responseDestination);
                return textMessage;
            }
        });
    }
}

消费者

package com.web.jms.service;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

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

@Service
public class ConsumerService {

    @Resource(name = "jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    public TextMessage receive(Destination destination){
        TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
        try{
            System.out.println("从队列" + destination.toString() + "收到了消息:\t"
                    + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return textMessage;
    }
}

3.消费者监听

package com.web.jms.listener;

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

public class JmsListener implements MessageListener {

    public void onMessage(Message message) {
        TextMessage textMsg = (TextMessage) message;
        try {
            System.out.println("接收到了消息,消息内容是:" + textMsg.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

4.errorhandler

package com.web.jms.handler;

import org.springframework.stereotype.Service;
import org.springframework.util.ErrorHandler;

@Service
public class JmsErrorHandler implements ErrorHandler{

    public void handleError(Throwable throwable) {
        System.out.println("出错了!");
        throwable.printStackTrace();
    }
}

5.配置applicationContext-ActiveMQ.xml





    

    
    
    
    

    
    
    
        
        
        
        
        
        
    

    

    
    
        
        
        
        
        
    

    

    

    
    

    
    
        
        
        
        
    

    

    

    
    
        
            queue
        
    


6.web.xml中添加启动文件


  
    contextConfigLocation
    
      classpath:config/applicationContext.xml,
      classpath:config/applicationContext-ActiveMQ.xml
    
  

7.controller

package com.web.jms.controller;

import com.web.jms.service.ConsumerService;
import com.web.jms.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.TextMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/jms")
public class JmsController {

    @Resource(name = "queueDestination")
    private Destination destination;

    //队列消息生产者
    @Autowired
    private ProducerService producer;

    //队列消息消费者
    @Autowired
    private ConsumerService consumer;

    @RequestMapping(value = "/SendMessage", method = RequestMethod.GET)
    @ResponseBody
    public void send() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss");
        String msg = sdf.format(new Date());
        System.out.println(Thread.currentThread().getName()+"------------send to jms Start");
        producer.sendMessage(msg);
        System.out.println(Thread.currentThread().getName()+"------------send to jms End");
    }

    @RequestMapping(value= "/ReceiveMessage",method = RequestMethod.GET)
    @ResponseBody
    public void receive(){
        System.out.println(Thread.currentThread().getName()+"------------receive from jms Start");
        TextMessage tm = consumer.receive(destination);
        System.out.println(Thread.currentThread().getName()+"------------receive from jms End");
    }
}

-end-

你可能感兴趣的:(maven构建springmvc+activeMQ)