【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 @   翠微  阅读(772)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示