java客户端连接测试某个activemq的代码
时间: 2024-05-04 11:16:09 浏览: 131
好的,以下是Java客户端连接测试某个ActiveMQ的代码示例:
```java
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQClient {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "TEST_QUEUE";
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello World!");
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");
MessageConsumer consumer = session.createConsumer(destination);
Message receivedMessage = consumer.receive();
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
System.out.println("Received text message '" + textMessage.getText() + "'");
} else {
System.out.println("Received message was not a text message");
}
connection.close();
}
}
```
请注意,这只是一个示例代码,你需要根据你的具体情况进行修改。同时,请确保你已经正确导入了 ActiveMQ 的 JAR 包并且已经启动了 ActiveMQ 服务。
阅读全文