0

我在 Tomcat Web 服务器中有 3 个应用程序。所有 3 个应用程序都使用 activemq 连接来发送 JMS 消息。我只想找到来自特定应用程序的连接并在 java 或 servlet 中关闭它们,然后再从服务器取消部署我的应用程序。

但我检查并坚持了这一点。我没有找到任何方法来找出特定应用程序的连接。

public class ConnFactory {

    private static QueueConnectionFactory conFactory;
    private static final Class[] SSA = new Class[] { String.class };

    public static QueueConnectionFactory getSingleFactory() {
        if (conFactory == null) {
            try {
                conFactory = GetConnectionFactory();
            } catch (Exception e) {
                System.err.println("Exception in getSingleFactory....");
                e.printStackTrace();

            }
        }

        return conFactory;

    }

    public static QueueConnectionFactory GetConnectionFactory()
            throws Exception {
        Object o = Class.forName("org.apache.activemq.ActiveMQConnectionFactory", true, ConnFactory.class.getClassLoader()).newInstance();
        Method pm = o.getClass().getMethod("setBrokerURL", SSA);
        String cfgStr = "keepAlive=true&wireFormat.stackTraceEnabled=true";
        String transCfgStr = "maxReconnectAttempts=3&startupMaxReconnectAttempts=3&timeout=10000";
        pm.invoke(o, new Object[] { "failover://(tcp://" + "localhost" + ":"
                + "61616" + "?" + cfgStr + ")?" + transCfgStr });
        ActiveMQConnectionFactory cd = (ActiveMQConnectionFactory) o;
        RedeliveryPolicy rd = cd.getRedeliveryPolicy();
        rd.setMaximumRedeliveries(5);
        rd.setMaximumRedeliveryDelay(5000);
        rd.setInitialRedeliveryDelay(5000);
        cd.setRedeliveryPolicy(rd);
        QueueConnectionFactory conf = cd;
        System.out.println("ConnectionFactory Initialized.......");
        return conf;
    }

    public QueueConnection getConnection() throws Exception {
        QueueConnection con = null;
        con = getSingleFactory().createQueueConnection();
        // con.setClientID("leo1");
        return con;
    }

    public void sendJMSMsg(QueueConnection qConn) throws Exception {

        TextMessage message = null;

        try {
            QueueSession queueSession = qConn.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            javax.jms.Queue queue1 = queueSession.createQueue("JQ1");
            QueueSender queueSender = queueSession.createSender(queue1);

            message = queueSession.createTextMessage();

            message.setText("This is message " + (new Date().getTime()));
            System.out.println("Sending message: " + message.getText());
            queueSender.send(message);
            qConn.close();
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        }

    }
}
4

1 回答 1

1

我也使用activemq很长一段时间只是一个初学者,可能是我的回答可以帮助你。首先,即使您使用多个应用程序与单个 activemq 一起使用,您的 3 个应用程序是否使用相同的队列?一种解决方案是在 activemq 代理中为您的应用程序使用多个队列。因此,如果您在 JMShandler 中收到一条消息,您可以取消部署您的应用程序。即使他们使用相同的队列,您也可以在每个应用程序的消息中添加一个属性。并且您可以在取消部署之前在您的客户端 java 应用程序中验证这一点。

于 2012-11-01T03:42:07.457 回答