嗨,我有 java 代码,它有一个生产者和一个消费者。生产者将消息发送到队列,消费者也收到消息,但我不知道为什么不显示消息。我正在使用 activeMQ 并且因为我没有收到消息所以我在 http:/localhost:8161/admin/queues.jsp 中签入,它显示队列有生产者和消费者但是消息没有打印在命令提示符。请告诉我
生产者.java
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "test";
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("this is a g message");
producer.send(message);
System.out.println("Sent message '" + message.getText() + "'");
System.out.println( ActiveMQConnection.DEFAULT_BROKER_URL);
connection.close();
}
}
消费者.java
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "test";
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);
MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '"
+ textMessage.getText() + "'");
}
connection.close();
}
}