本帖最后由 hanjiaming 于 2012-07-27 16:43:20 编辑

解决方案 »

  1.   

    代码如下:Comsumerpublic class Comsumer { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String user = ActiveMQConnection.DEFAULT_USER;
    String password = ActiveMQConnection.DEFAULT_PASSWORD;
    String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    // String url="vm:(broker:(tcp://localhost:61616))";
    // String url="vm://localhost";
    String subject = "TOOL.DEFAULT";
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url);
    Connection connection;
    try {
    connection = connectionFactory.createConnection();
    connection.start();
    final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(subject);
    MessageConsumer message = session.createConsumer(destination);
    message.setMessageListener(new MessageListener() {
    public void onMessage(Message msg) {
    MapMessage message = (MapMessage) msg;
    try {
    System.out.println("--收到消息:" + new Date(message.getLong("count")));
    session.commit();
    } catch (JMSException e) {
    e.printStackTrace();
    }
    }
    });
    Thread.sleep(30000);
    session.close();
    connection.close();
    } catch (JMSException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } }}Producer:public class Producer {
    public static void main(String[] args) {
    String user = ActiveMQConnection.DEFAULT_USER;
    String password = ActiveMQConnection.DEFAULT_PASSWORD;
    String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    // String url="vm:(broker:(tcp://localhost:61616))";
    // String url = "tcp://localhost:61616";
    // String url="vm://localhost";
    String subject = "TOOL.DEFAULT";
    ConnectionFactory contectionFactory = new ActiveMQConnectionFactory(
    user, password, url);
    try {
    Connection connection = contectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(Boolean.TRUE,
    Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(subject);
    MessageProducer producer = session.createProducer(destination);
    for (int i = 0; i <= 20; i++) {
    MapMessage message = session.createMapMessage();
    Date date = new Date();
    message.setLong("count", date.getTime());
    Thread.sleep(1000);
    producer.send(message);
    System.out.println("--发送消息:" + date);
    }
    session.commit();
    session.close();
    connection.close();
    } catch (JMSException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
      

  2.   

    楼主解决问题了吗?貌似是没开activemq.bat的原因吧