Java调用ActiveMQ简单范例的代码

如下的内容是关于Java调用ActiveMQ简单范例的内容,应该能对大伙有所用。

package jms;  

import javax.jms.Connection;  
import javax.jms.DeliveryMode;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.MessageProducer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnectionFactory;  
import org.apache.activemq.broker.BrokerService;  
import org.apache.log4j.PropertyConfigurator;  

public class PTP_sends {  

    public PTP_sends(){  

    }  

    private Connection connection;  
    private String subject = "TOOL.DEFAULT";  

    public void send(String msg){  

        try {  
            connection=connectionFactory.createConnection();  
            connection.start();  
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
            Destination destination=session.createQueue(subject);  
            MessageProducer producer=session.createProducer(destination);  
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
            TextMessage message = session.createTextMessage(msg);  

            producer.send(message);  
            System.out.println("消息已经发送。。。。");  

            message.clearProperties();  
            session.close();  
            connection.stop();  
            connection.close();  
            System.out.println("关闭资源。。。。");  
        } catch (JMSException e) {  
            e.printStackTrace();  
        }  
    }  

    public static void main(String[] args) {  
        PropertyConfigurator.configure("c:\log4j.properties");  

        PTP_sends ptpSends=new PTP_sends();  
        ptpSends.send("this is JMS .....");  
    }  

}  

package jms;  

import javax.jms.Connection;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnectionFactory;  
import org.apache.log4j.PropertyConfigurator;  

public class PTP_receive {  

    private Connection connection;  
    private String subject = "TOOL.DEFAULT";  

     public void receive(){  
            ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory();  
            try {  
                connection=connectionFactory.createConnection();  
                connection.start();  
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
                Destination destination=session.createQueue(subject);  
                MessageConsumer consumer=session.createConsumer(destination);  
                System.out.println("同步接受消息:");  
                Message message=consumer.receive();  
                System.out.println("n收到的message 是:"+((TextMessage)message).getText());  

                message.clearProperties();  
                consumer.close();  
                session.close();  
                connection.stop();  
                connection.close();  
                System.out.println("关闭资源。。。。");  
            } catch (JMSException e) {  
                e.printStackTrace();  
            }  
        }  

    public static void main(String[] args) {  
        PropertyConfigurator.configure("c:\log4j.properties");  
        PTP_receive receive=new PTP_receive();  
        receive.receive();  
    }  

}  
Java中实现ActiveMQ的订阅和消费,通常会使用Apache ActiveMQ的客户端API。以下是一个简单示例,展示了如何创建一个消费者连接ActiveMQ队列并接收消息: ```java import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.MessageConsumer; import javax.jms.Session; public class ActiveMQConsumer { private static final String QUEUE_NAME = "exampleQueue"; // 队列名称 public static void main(String[] args) { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // Broker地址及端口 try { Connection connection = connectionFactory.createConnection(); connection.start(); // 开始连接 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 非持久化、自动确认模式 Destination destination = session.createQueue(QUEUE_NAME); MessageConsumer consumer = session.createConsumer(destination); System.out.println("Subscribed to queue: " + destination); while (true) { javax.jms.Message message = consumer.receive(5000); // 等待接收消息,超时时间为5秒 if (message != null) { String text = ((TextMessage) message).getText(); // 提取文本消息内容 System.out.println("Received message: " + text); message.acknowledge(); // 手动确认接收到的消息 } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } } } ``` 在这个例子中,我们首先创建了一个`ConnectionFactory`,然后建立连接并创建一个会话。接着,我们创建一个消费者来订阅指定的队列。程序将进入一个无限循环,在其中等待并处理从队列接收到的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值