ActiveMQDemo

ActiveMQ是实现JMS API的框架,适合中小型项目。demo采用textMessage方式分别叙述队列模式和主题模式。

 

生产者配置:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import com.fan.activeMQ.service.producer.ProducerService;
import com.fan.activeMQ.service.producer.impl.ProducerServiceImpl;

@Configuration
public class AvticeMQProducerConfig {
    // @Value("${spring.activemq.url}")
    String url = "tcp://127.0.0.1:61616";

    /**
     * 配置jms 的链接工厂
     * 
     * @return
     */
    @Bean
    public SingleConnectionFactory ConnectionFactory() {
        return new SingleConnectionFactory(activeMQConnectionFactory());
    }

    /**
     * 配置activemq 的链接工厂
     * 
     * @return
     */
    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        return new ActiveMQConnectionFactory(url);
    }

    /**
     * 队列模式
     * 
     * @return
     */
    @Bean
    public ActiveMQQueue queueDestination() {
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("queue-test2");
        return activeMQQueue;
    }
    /**
     * 主题模式
     * @return
     */
    @Bean
    public ActiveMQTopic topicDestination() {
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("topic-test2");
        return activeMQTopic;
    }
    /**
     * 模板类
     * @return
     */
    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(ConnectionFactory());
        return jmsTemplate;
    }
    /**
     * 实现类
     * @return
     */
    @Bean
    public ProducerService producerServiceImpl() {
        return new ProducerServiceImpl();
    }
}

 

消费者配置:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import com.fan.activeMQ.service.consumer.ConsumerMessageLinster;

//extends ActiveMQConfig
@Configuration
public class AvticeMQConsumerConfig {
    // @Value("${spring.activemq.url}")
    String url = "tcp://127.0.0.1:61616";

    /**
     * 配置jms 的链接工厂
     * 
     * @return
     */
    @Bean
    public SingleConnectionFactory ConnectionFactory() {
        return new SingleConnectionFactory(activeMQConnectionFactory());
    }

    /**
     * 配置activemq 的链接工厂
     * 
     * @return
     */
    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        return new ActiveMQConnectionFactory(url);
    }

    /**
     * 队列模式
     * 
     * @return
     */
    @Bean
    public ActiveMQQueue queueDestination() {
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("queue-test2");
        return activeMQQueue;
    }
    /**
     * 主题模式
     * @return
     */
    @Bean
    public ActiveMQTopic topicDestination() {
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("topic-test2");
        return activeMQTopic;
    }

    /**
     * 监听
     */
    @Bean
    public DefaultMessageListenerContainer jmsContainer() {
        DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer();
        listenerContainer.setConnectionFactory(ConnectionFactory());
        listenerContainer.setMessageListener(consumerMessageListener());
        listenerContainer.setDestination(queueDestination());
        listenerContainer.setDestination(topicDestination());
        return listenerContainer;
    }
    /**
     * 模板类
     * @return
     */
    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate(ConnectionFactory());
        return jmsTemplate;
    }
    /**
     * 实现类
     * @return
     */
    @Bean
    public ConsumerMessageLinster consumerMessageListener() {
        return new ConsumerMessageLinster();
    }
}

 

生产者service:

public interface ProducerService {
    public void sendMessage(String textMessage);
}

实现:

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.fan.activeMQ.service.producer.ProducerService;

//@Service
public class ProducerServiceImpl implements ProducerService {
    @Autowired
    JmsTemplate JmsTemplate;
//    @Resource(name = "queueDestination")
    @Resource(name = "topicDestination")
    Destination destination;

    @Override
    public void sendMessage(String textMessage) {
        JmsTemplate.send(destination, new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage createTextMessage = session.createTextMessage(textMessage);
                return createTextMessage;
            }
        });
        System.out.println("發送消息" + textMessage);

    }
}

 

消费者:

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

public class ConsumerMessageLinster implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("接收的消息"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
        
    }
}

生产者test:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.fan.activeMQ.config.AvticeMQProducerConfig;
import com.fan.activeMQ.service.producer.impl.ProducerServiceImpl;

public class TestProducer {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AvticeMQProducerConfig.class);
        ProducerServiceImpl bean = applicationContext.getBean(ProducerServiceImpl.class);
        for (int i = 0; i < 100; i++) {
            bean.sendMessage("发送消息:条数" + i);
        }
        AnnotationConfigApplicationContext annotationConfigApplicationContext =  (AnnotationConfigApplicationContext)applicationContext;
        annotationConfigApplicationContext.close();
    }
}

消费者test:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.fan.activeMQ.config.AvticeMQConsumerConfig;

public class TestConsumer {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AvticeMQConsumerConfig.class);
        
    }
}

注意:当测试队列模式的时候首先启动生产者,但后由消费者消费;当启动主题模式的时候,首先启动消费者来订阅,再启动生产者。

posted @ 2018-10-23 17:16  FN飞鸟  阅读(159)  评论(0编辑  收藏  举报