【Java Web开发学习】Spring消息-ActiveMQ发送消息

ActiveMQ发送消息

转载:http://www.cnblogs.com/yangchongxing/p/9042401.html

Java消息服务(Java Message Service, JMS)是一个Java标准,定义了使用消息代理的通用API。
ActiveMQ是一个开源消息代理产品,也是使用JMS进行异步消息传递的最佳选择。
下载ActiveMQ:http://activemq.apache.org
下载解压后进入相应的bin目录:activemq start

借助JMS连接工厂通过消息代理发送消息

创建JMS连接工厂
默认的账户:
ActiveMQConnection.DEFAULT_USER
ActiveMQConnection.DEFAULT_PASSWORD
ActiveMQConnection.DEFAULT_BROKER_URL

Java配置

@Bean
public ConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
    factory.setBrokerURL("tcp://localhost:61616");
    return factory;
}

XML配置

<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory" p:brokerURL="tcp://localhost:61616"/>

声明消息目的地

队列

Java配置

@Bean
public ActiveMQQueue queue() {
    ActiveMQQueue queue = new ActiveMQQueue();
    queue.setPhysicalName("mvc.queue");
    return queue;
}

XML配置

<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue" c:_="mvc.queue"/>

主题

Java配置

@Bean
public ActiveMQTopic topic() {
    ActiveMQTopic topic = new ActiveMQTopic();
    topic.setPhysicalName("mvc.topic");
    return topic;
}

XML配置

<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic" c:_="mvc.topic"/>

使用传统JMS方式发送和接受消息

public void sendMsg() throws Exception {
    Connection conn = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = new ActiveMQQueue("mvc.queue");
    MessageProducer producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage();
    message.setText("hello");
    producer.send(message);
    session.close();
    conn.close();
}
public void receiveMsg() throws Exception {
    Connection conn = new ActiveMQConnectionFactory("tcp://localhost:61616");
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = new ActiveMQQueue("mvc.queue");
    MessageConsumer consumer = session.createConsumer(destination);
    TextMessage message = (TextMessage) consumer.receive();
    System.out.println(message.getText());
    session.close();
    conn.close();
}

定义JmsTemplate对象
Java配置

@Bean
public JmsTemplate jmsTemlate(ConnectionFactory connectionFactory, ActiveMQQueue queue) {
    JmsTemplate template = new JmsTemplate(connectionFactory);
    template.setDefaultDestination(queue);
    return template;
}

XML配置

<bean id="jmsTemlate" class="org.springframework.jms.core.JmsTemplate" c:_-ref="connectionFactory" p:defaultDestination="queue"/>

使用JMS模板方式发送和接受消息

public void sendMsg() {
    jmsOperations.send(new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            TextMessage message = session.createTextMessage();
            message.setText("Hello Jms.");
            return message;
        }
    });
}
public void receiveMsg() throws JMSException {
    TextMessage message = (TextMessage) jmsOperations.receive();
    System.out.println(message.getText());
}

使用Convert

public void sendMsg() {
    jmsOperations.convertAndSend("Hello");
}
public void receiveMsg() {
    String message = (String) jmsOperations.receiveAndConvert();
    System.out.println(message);
}

待续...

posted @ 2018-05-15 18:23  翠微  阅读(767)  评论(0编辑  收藏  举报