要解决"ActiveMQ待处理消息"的问题,您可以使用以下代码示例:
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class ActiveMQConsumer {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建目的地(队列或主题)
Destination destination = session.createQueue("your_queue_name");
// 创建消费者
MessageConsumer consumer = session.createConsumer(destination);
// 接收消息
Message message = consumer.receive();
// 处理消息
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
// 关闭连接
consumer.close();
session.close();
connection.close();
}
}
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class ActiveMQProducer {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建目的地(队列或主题)
Destination destination = session.createQueue("your_queue_name");
// 创建消息生产者
MessageProducer producer = session.createProducer(destination);
// 创建消息
TextMessage message = session.createTextMessage("Hello ActiveMQ!");
// 发送消息
producer.send(message);
// 关闭连接
producer.close();
session.close();
connection.close();
}
}
请确保在代码中设置正确的ActiveMQ连接URL和消息队列名称。使用这些代码示例,您可以接收和发送ActiveMQ消息并进行处理。