用myeclipse,可以发送,但接收不到。
public class AppProducer { private static final String url="tcp://127.0.0.1:61616"; private static final String queueName="queue-test"; public static void main(String[] args) throws JMSException { //1.创建ConnectionFactory连接工厂 ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url); //2.创建连接 Connection connection =connectionFactory.createConnection(); //3.启动连接 connection.start(); //4.创建会话 Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.创建目标 Destination destination= session.createQueue(queueName); //6.创建生产者 MessageProducer messageProducer= session.createProducer(destination); for(int i=0;i<100;i++){ //7.创建消息 TextMessage textMeasurer=session.createTextMessage("test: "+i); //8.发布消息 messageProducer.send(textMeasurer); System.out.println("发送消息:"+textMeasurer.getText()); } //9.关闭连接 connection.stop(); } }
public class AppConsumer { private static final String url="tcp://127.0.0.1:61616"; private static final String queueName="queue-test"; public static void main(String[] args) throws JMSException { //1.创建ConnectionFactory连接工厂 ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url); //2.创建连接 Connection connection =connectionFactory.createConnection(); //3.启动连接 connection.start(); //4.创建会话 Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5.创建目标 Destination destination= session.createQueue(queueName); //6.创建消费者 MessageConsumer messageConsumer = session.createConsumer(destination); //7.创建一个监听器 messageConsumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage textMessage= (TextMessage) message; try { System.out.println("接收消息: "+textMessage.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); //8.关闭连接 //connection.stop(); } }
AppProducer可以生产发送到中间件
但是AppConsumer接收不到