public class MessageReceiver {
// URL of the JMS server
private static String url = "tcp://atuleusbduv012.aemud.com:61616";
// default broker URL is : tcp://localhost:61616"
// Name of the queue we will receive messages from
private static String subject = "activemq.test.incoming.master";
// Queue Name.You can create any/many queue names as per your requirement.
public static void main(String[] args) throws JMSException, InterruptedException {
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("agile","123456789",url);
//ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
// Creating session for sending messages
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
System.out.println("session :: "+session.toString());
// Getting the queue 'amd.tmpesb.sapphire.incoming.master'
Destination destination = session.createQueue(subject);
System.out.println("destination :: "+destination.toString());
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = session.createConsumer(destination);
System.out.println("consumer :: "+consumer.toString());
// Here we receive the message.
Message message = consumer.receive();
System.out.println("message :: "+message.toString());
// We will be using TestMessage in our example. MessageProducer sent us a TextMessage
// so we must cast to it to get access to its .getText() method.
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText() + "'");
}
connection.close();
}
}
上面的代码不是从队列中读取消息,当我调试java类时,我发现“执行在consumer.receive();
方法停止,即它不断尝试读取消息但不能读取。
请告诉我,上面MessageReceiver
课程
还有关于向上面的队列发送/写入消息的更多细节。
我们使用下面的类将消息发送到队列activemq.test.incoming.master
并正在成功发送消息
public class MessageSender {
//URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
private static String url = "tcp://atuleusbduv012.aemud.com:61616";
//private static String url = "tcp://localhost:61616";
// default broker URL is : tcp://localhost:61616"
private static String subject = "activemq.test.incoming.master";
// Queue Name.You can create any/many queue names as per your requirement.
public static void main(String[] args) throws JMSException {
// Getting JMS connection from the server and starting it
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("agile","123456789",url);
//ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
//Creating a non transactional session to send/receive JMS message.
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//Destination represents here our queue 'JCG_QUEUE' on the JMS server.
//The queue will be created automatically on the server.
Destination destination = session.createQueue(subject);
// MessageProducer is used for sending messages to the queue.
MessageProducer producer = session.createProducer(destination);
// We will send a small text message saying 'Hello World!!!'
TextMessage message = session.createTextMessage("Hello !!! Welcome to the world of ActiveMQ.");
// Here we are sending our message!
producer.send(message);
System.out.println("JCG printing@@ '" + message.getText() + "'");
connection.close();
}
}
我请求您告诉我上述MessageReceiver
课程中缺少的内容,因为它没有从队列activemq.test.incoming.master
中读取消息。
答案 0 :(得分:1)
我使用线程概念来读取和写入队列的消息。
参考链接http://activemq.apache.org/hello-world.html
receiver
可以等待一段时间来接收消息consumer.receive(3000)
,并且在尝试接收等待一段时间后使用Thread.sleep(300)
生成更多消息,我们将能够读取和写入消息到远程机器中的队列。
全部谢谢