0

I am having 3 applications in Tomcat Web server.All 3 apps are using activemq connections to send JMS messages .I want to find only connections from a particular application and close them in java or servlet before undeploy my application from server.

But I checked and stuck with that.I didn't find any methods to find out connections from particular application.

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());
        }

    }
}
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • how do you setup the connections in the first pplace? – Aksel Willgert Nov 01 '12 at 06:17
  • @AkselWillgert sorry for adding entire code but did for clarity and thank you for your help. – sunleo Nov 01 '12 at 06:53
  • Sorry i misunderstood the code, so i deleted my answer. Is the problem that connections are left hanging because app is undeployed sometime between calling getConnection() and sendJMSMsg() – Aksel Willgert Nov 01 '12 at 08:51
  • see there are 100 connections created but am going to undeploy application and dont bother about connection before undeploy I have to close all 100 connections.You understood 95% I hope. – sunleo Nov 01 '12 at 09:05
  • 1
    but according to your code connections are closed as soon as you send a message (unless you get an exception which leaves the connection hanging) – Aksel Willgert Nov 01 '12 at 17:16
  • but if sendMsg is called and not closed connection then what will happen? – sunleo Nov 02 '12 at 01:57
  • why do you need to explicitly close client connections? – Ben ODay Nov 06 '12 at 05:41
  • if i have situation to shutdown or undeploy application then JMS connections will abruptly hanging so how to avoid that. – sunleo Nov 06 '12 at 05:56

1 Answers1

1

Am also using activemq for quite some time a beginner only, may be my answer can help you. First of all, even if you use multiple applications working with single activemq, does 3 of your apps using the same queue? one solution is using multiple queues for your apps in an activemq broker. So that if you receive a message in your JMShandler you can undeploy your app. Even if they use same queue, you can add a property in your message from each of your app. and you can verify that in your client java application before undeploying.

Ram
  • 11
  • 1
  • am using different queues for different apps and what I want to know is how to close the opened connections ?if you can redirect to some other references also. – sunleo Nov 01 '12 at 03:48