Active MQ Producer在send()方法上无限期挂起

时间:2015-07-30 12:39:08

标签: java activemq

我在异步模式下使用活动的mq producer-subscriber模型来编写一个简单的测试程序。我首先运行我的消费者代码,然后运行生产者代码。虽然cosumer运行正常,但生成器代码在调用send()方法时无限期挂起。我在下面给出了相关的代码片段:

制片人类:

public void produce() {

    try {
        con = MyConnection.getInstance().getConnection();
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        dest = session.createQueue("som.mq");
        producer = session.createProducer(dest);
        con.start();
        File file = new File("Message.xml");
        message = session.createObjectMessage();
        fis = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        fis.read(buffer);
        message.setObject(buffer);
        producer.send(message);

    } catch (Exception e) {
        System.out.println("Exception caught " + e);
    }

}

消费者类:

public void consume() {

    try {
        con = MyConnection.getInstance().getConnection();
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        dest = session.createQueue("som.mq");
        consumer = session.createConsumer(dest);
        MessageListener listener = new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof ObjectMessage) {
                        ObjectMessage myMessage = (ObjectMessage) message;
                        byte[] myByte = (byte[]) myMessage.getObject();
                        for (int i = 0; i < myByte.length; i++) {
                            System.out.print((char) myByte[i]);
                        }
                    } else
                        System.out.println(message);
                } catch (JMSException ex) {
                    System.out.println("Caught JMS Exception " + ex);
                }
            }
        };
        session.setMessageListener(listener);
        con.start();

    } catch (Exception e) {
        System.out.println("Exception caught " + e);
    }
}

MyConnection Class

public class MyConnection {

private static transient MyConnection myConnection;
private PooledConnectionFactory connectionFactory;
private Connection connection;

private MyConnection() {

        String url = "tcp://localhost:61616";
        connectionFactory = new PooledConnectionFactory(url);
        connectionFactory.setMaxConnections(8);

}

public static MyConnection getInstance() {
    if (myConnection == null) {
        synchronized (MyConnection.class) {
            if (myConnection == null) {
                myConnection = new MyConnection();
            }
        }

    }
    return myConnection;
}

public Connection getConnection() throws JMSException {
    System.out.println("Printing connection "+connectionFactory.createConnection().toString());
    connection = connectionFactory.createConnection();
    return connection;
}

}

我在控制台输出上没有例外。感谢有人让我知道我可能缺少什么或编码错误。

0 个答案:

没有答案