spring+activemq

1.pom文件

 


	4.0.0
	com.huawei
	activeMqTest
	0.0.1-SNAPSHOT
	war

	
		
		4.1.8.RELEASE
		
		1.6.6
		1.2.12
		
		4.10
		
		3.2.1
	
	
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.apache.xbean
			xbean-spring
			3.16
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
			org.springframework
			spring-tx
			${spring.version}
		
		
			org.springframework
			spring-jdbc
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		

		
		
			junit
			junit
			${junit.version}
			test
		

		
		
		
			log4j
			log4j
			${log4j.version}
		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		

		
		
			org.springframework
			spring-test
			${spring.version}
			test
		

		
		
			org.mybatis
			mybatis
			${mybatis.version}
		

		
		
			org.mybatis
			mybatis-spring
			1.2.0
		

		
		
			mysql
			mysql-connector-java
			5.1.29
		

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

		
		
			org.springframework
			spring-jms
			${spring.version}
		
		
			org.springframework
			spring-messaging
			${spring.version}
		

		
		
			com.alibaba
			fastjson
			1.2.31
		
	

	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
				
			
		
	

2.web.xml



  activeMqTest
  
    
      
        contextConfigLocation  
        classpath:applicationContext.xml,classpath:spring_activemq.xml  
      
      
      
        org.springframework.web.context.ContextLoaderListener  
    
    
      
        springDispatcherServlet  
        org.springframework.web.servlet.DispatcherServlet  
          
            contextConfigLocation  
            classpath:springmvc.xml  
        
        1  
      
      
      
        springDispatcherServlet  
        /  
      
      
    
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

3.application.xml

 

 

  
   
  
      
         
      
  
      
      
          
          
          
          
     
  


4.spring_activemq.xml

 

 



        
	
	

	
	
		
		
	

	
	
		
		
			activemq..demo
		
	

	
	
		
		
		
		
		
	

	
	

	
	
		
		
		
	


5.springmvc.xml

 

 

  
  
      
  
      
          
          
      
  
      
          
      
  


6.目錄結構

 

spring+activemq_第1张图片

7.java文件

controller/Test.java

 

package com.senge.controller;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huawei.queue.producer.ProducerMessage;

@Controller
public class Test {
    @Autowired
    private BeanFactory factory;

    @RequestMapping(value = "/test")
    public void testactivemq() {
        ProducerMessage proMess = factory.getBean(ProducerMessage.class);
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                proMess.sendMessage("haha" + i);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

queue/consumer/QueueMessageListener.java  配置文件默認隊列名

 

 

package com.senge.queue.consumer;

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

import org.springframework.stereotype.Component;

@Component
public class QueueMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("QueueMessageListener监听到了文本消息:\t" + tm.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

queue/consumer/Senyansen.java

package com.senge.queue.consumer;

import javax.annotation.Resource;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class Senyansen implements MessageListener{
    @Resource(name="jmsTemplate")  
    private JmsTemplate jmsTemplate;  
    @Override
    public void onMessage(Message arg0) {
        String destination = (String)new ActiveMQQueue("shiyansen").toString();
        TextMessage tm = (TextMessage) jmsTemplate.receive(destination);  
        System.out.println(tm.toString());
        
    }

}


queue.producer.ProducerMessage

 

 

 

 

package com.senge.queue.producer;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

@Service
public class ProducerMessage {
    @Resource(name = "jmsTemplate")
    private JmsTemplate jmsTemplate;

    /**
     * 向指定队列发送消息
     */
    public void sendMessage(final String msg) {

        // String destination = jmsTemplate.getDefaultDestination().toString();
        String destination = (String) new ActiveMQQueue("shiyansen").toString();
        System.out.println("向队列" + destination + "发送了消息------------" + msg);
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }
}

 

 

 

 

 

 

 


 

 

你可能感兴趣的:(activeMq)