博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用maven和spring搭建ActiveMQ环境
阅读量:5043 次
发布时间:2019-06-12

本文共 11489 字,大约阅读时间需要 38 分钟。

 

前面搭建过了简单的环境,这次用稍微实际一点的maven+spring+activemq来进行搭建

 

准备:win7,eclipse,jdk1.8,tomcat8,maven3.5.2,activemq5.9

开始:建maven项目就不说了,看结构图

 

 

pom.xml

4.0.0
com.sinosoft
mq
war
0.0.1-SNAPSHOT
mq Maven Webapp
http://maven.apache.org
4.2.1.RELEASE
junit
junit
3.8.1
test
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-core
${springframework}
org.springframework
spring-context
${springframework}
org.springframework
spring-tx
${springframework}
org.springframework
spring-webmvc
${springframework}
org.springframework
spring-web
${springframework}
org.springframework
spring-jms
${springframework}
org.apache.xbean
xbean-spring
3.16
com.thoughtworks.xstream
xstream
1.3.1
org.apache.activemq
activemq-all
5.9.0
mq

 

web.xml

mq
contextConfigLocation
classpath:activemq.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc-dispatch.xml
1
springMVC
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
true
forceResponseEncoding
true
characterEncodingFilter
/*

 

activemq.xml

sinosoft

 

springmvc.xml

 

WelcomeController.java

package com.sinosoft.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class WelcomeController {    @RequestMapping(value="/welcome",method=RequestMethod.GET)    public ModelAndView welcome(){        System.out.println("-------------welcome-----------");        ModelAndView mv = new ModelAndView();        mv.setViewName("welcome");        return mv;    }}

 

DemoController.java

package com.sinosoft.controller;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.annotation.Resource;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.TextMessage;import javax.management.MBeanServerConnection;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXServiceURL;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.RequestParam;import org.springframework.web.servlet.ModelAndView;import com.sinosoft.activemq.consumer.ConsumerService;import com.sinosoft.activemq.producer.ProducerService;@Controllerpublic class DemoController {//    @Resource(name = "demoQueueDestination")    @Autowired    private Destination demoQueueDestination;//    @Resource(name = "producerService")    @Autowired    private ProducerService producer;//    @Resource(name = "consumerService")    @Autowired    private ConsumerService consumer;    @RequestMapping(value = "/producer", method = RequestMethod.GET)    public ModelAndView producer() {        System.out.println("--------------go producer");        Date now = new Date();        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String time = sdf.format(now);        System.out.println(time);        ModelAndView mv = new ModelAndView();        mv.addObject("time", time);        mv.setViewName("jms_producer");        return mv;    }    @RequestMapping(value = "/onsend", method = RequestMethod.POST)    public ModelAndView producer(@RequestParam("message") String message) {        System.out.println("---------send to jms");        ModelAndView mv = new ModelAndView();        producer.sendMessage(demoQueueDestination, message);        mv.setViewName("welcome");        return mv;    }    @RequestMapping(value = "/receive", method = RequestMethod.GET)    public ModelAndView queue_receive() throws JMSException {        System.out.println("-----------receive message");        ModelAndView mv = new ModelAndView();        TextMessage tm = consumer.receive(demoQueueDestination);        if (tm == null) {            mv.addObject("textMessage", "Queue is Empty");        } else {            mv.addObject("textMessage", tm.getText());        }        mv.setViewName("jms_receiver");        return mv;    }    public ModelAndView jmsManager() throws IOException {        System.out.println("--------------jms manager");        ModelAndView mv = new ModelAndView();        mv.setViewName("welcome");        JMXServiceURL url = new JMXServiceURL("");        JMXConnector connector = JMXConnectorFactory.connect(url);        connector.connect();        MBeanServerConnection connection = connector.getMBeanServerConnection();        return mv;    }}

 

ProducerService.java

package com.sinosoft.activemq.producer;import javax.annotation.Resource;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service;@Servicepublic class ProducerService {//    @Resource(name="jmsTemplate")    @Autowired    private JmsTemplate jmsTemplate;            public void sendMessage(Destination destination,final String msg){        System.out.println("Send " + msg + " to Destination " + destination.toString());                MessageCreator messageCreator = new MessageCreator(){            public Message createMessage(Session session) throws JMSException {                return session.createTextMessage(msg);            }                    };                jmsTemplate.send(destination, messageCreator);    }            public void sendMessage(final String msg){                String destination = jmsTemplate.getDefaultDestinationName().toString();        System.out.println("Send " + msg + " to Destination " + destination);        MessageCreator messageCreator = new MessageCreator(){            public Message createMessage(Session session) throws JMSException {                // TODO Auto-generated method stub                return session.createTextMessage(msg);            }                    };                jmsTemplate.send(messageCreator);    }}

 

ConsumerService.java

package com.sinosoft.activemq.consumer;import javax.annotation.Resource;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.TextMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Service;@Servicepublic class ConsumerService {//    @Resource(name = "jmsTemplate")    @Autowired    private JmsTemplate jmsTemplate;    public TextMessage receive(Destination destination) {        TextMessage tm = (TextMessage) jmsTemplate.receive(destination);        if (tm != null) {            try {                System.out.println("Get Message: " + tm.getText() + " from Destination " + destination.toString());            } catch (JMSException e) {                e.printStackTrace();            }        }        return tm;    }}

 

好了,先把activemq运行上

 

 接着运行tomcat后进入页面看效果,这里只看点对点传输

 

 

控制台打印日志

 

 最后说一说可能遇上的问题及解决方式

 

 

如果没有上图第五行这个,可能会报找不到监听器的错误,只需自己添上就行,add->java build path enties->maven dependencies

 

如果第一次tomcat没有运行成功中途报错了,下一次就算代码正确了,或者配置都正确了也有可能会运行不起来,因为tomcat有缓存,所以清除tomcat下work下所有文件很有必要.其次把webapp里面无关紧要的项目都清除掉,这会大大降低tomcat的启动时间.

 

 

转载于:https://www.cnblogs.com/goujh/p/8532439.html

你可能感兴趣的文章