Spring整合ActiveMQ

1.pom依赖

properties>
        UTF-8
        UTF-8

        1.8
        1.8
        1.8

        1.8

        5.1.2.RELEASE

        

    

    
        
            dev
            
                dev
            
            
                true
                1.8
            
            
                
                    src/main/resource/dev.properties
                
            
        
    

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

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

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


        
            junit
            junit
            4.12
        

        
        
            org.slf4j
            slf4j-log4j12
            1.7.25
            test
        

        
            org.apache.activemq
            activemq-core
            5.7.0
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    spring-context
                    org.springframework
                
                
                    slf4j-api
                    org.slf4j
                
            
        

        
        
            org.apache.xbean
            xbean-spring
            4.9
        

        
            javax.jms
            javax.jms-api
            2.0.1
        

        
            org.apache.activemq
            activemq-pool
            5.9.0
        

    

2.ActiveMQ.xml配置





    

    


    
    
        
            
                
                     tcp://localhost:61616
                
            
        
        
    

    
    
           
           
    

    

    
    
        
        
    
    
    
        
        
    

    

    

  


    
    
    
        
        
    

    
    
        
        
    

3.生成者实现

@Service
public class QueueSender {



    private JmsTemplate jmsTemplate;

    @Autowired
    private QueueSender(@Qualifier("jmsQueueTemplate")JmsTemplate jmsTemplate){
        this.jmsTemplate = jmsTemplate;
    }


    public void send(String queueName, String message){

        jmsTemplate.send(queueName,(Session session)->{
            return session.createTextMessage(message);

        });



    }
}


@Service
public class TopicSender {


    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate jmsTemplate;


    public void send(String topicName,final String message){

        jmsTemplate.send(topicName,(Session session)->{
            return session.createTextMessage(message);
        });


    }

}

4.消费者实现

@Service("queueConsumer1")
public class QueueConsumer1 implements MessageListener{

    @Override
    public void onMessage(Message message) {
        TextMessage message1 = (TextMessage) message;

        try {
            System.out.println("消费者queueConsumer1获取消息:" + message1.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

@Service("topicConsumer1")
public class TopicConsumer1 implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage message1 = (TextMessage) message;
        try {
            System.out.println("消费者topicConsumer1获取消息:" + message1.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:amq-applicationContext.xml"})
public class ActiveMQ_Test {

    @Autowired
    private QueueSender queueSender;

    @Autowired
    private TopicSender topicSender;


    @Test
    public void testSendMessage(){

        queueSender.send("spring_queue","你好,我是queue producer");
        topicSender.send("spring_topic","你好,我是topic Producer");

        //这里跑个死循环,是不让进程挂掉,不然消息还没来得及消费,进程就结束了
        while(true){

        }

    }


}

 

你可能感兴趣的:(Spring整合ActiveMQ)