Reconnecting to the JMS server initially and after receiving the previous connection

I am having a problem with JMS re-communication. I am not sure if I am doing this correctly (most likely, I do not know). Anyway, I use WebLogic, and this is a consumer client. Here, as I get the connection, and trying to add automatic reconnection. The problem is that it does not reconnect, and there are no exceptions registering something-ever. I re-create the situation by starting weblogic, starting the client client that receives the initial connection, I close weblogic aberratively, then restart weblogic again, after which any messages in the queue that this consumer listens to remain in the queue without being acknowledged because they are not accepted.

public void setReceiver(MessageListener listener) {
        try {
            Properties parm = new Properties();
            parm.setProperty("java.naming.factory.initial",
                    "weblogic.jndi.WLInitialContextFactory");
            parm.setProperty("java.naming.provider.url", URL);
            parm.setProperty("java.naming.security.principal", username);
            parm.setProperty("java.naming.security.credentials", password);
            ctx = new InitialContext(parm);
            final QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx
                    .lookup(conFactoryName);
            connection = connectionFactory.createQueueConnection();
            // TODO: 8/6/2012 Work on reconnection strategies for Consumer.
            ((WLConnection) connection)
                    .setReconnectPolicy(JMSConstants.RECONNECT_POLICY_ALL);
            ((WLConnection) connection).setReconnectBlockingMillis(30000L);
            ((WLConnection) connection).setTotalReconnectPeriodMillis(-1L);
            session = connection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            queue = (Queue) ctx.lookup(queueName);
            // receiver = session.createReceiver(queue);
            // receiver.setMessageListener(listener);
            consumer = session.createConsumer(queue);
            consumer.setMessageListener(listener);

            connection.setExceptionListener(new ExceptionListener() {

                @Override
                public void onException(JMSException arg0) {
                    // Assume Disconnected.
                    FileHandler fh = null;
                    try {
                        fh = new FileHandler("./logs/ExceptionListener", true);
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    SimpleFormatter formatter = new SimpleFormatter();
                    fh.setFormatter(formatter);
                    Logger log2 = Logger.getLogger("ExceptionListener");
                    log2.addHandler(fh);
                    boolean connected = false;
                    do {
                        if (connection != null) {
                            try {
                                connection.close();
                            } catch (JMSException e) {
                                log.warning(e.toString());
                            }
                            try {
                                connection = connectionFactory.createQueueConnection();
                                connection.setExceptionListener(this);
                                connection.start();
                                connected = true;
                            } catch (JMSException e) {
                                log.severe(e.toString());
                            }
                        }
                    } while (!connected);

                }
            });
            connection.start();

        } catch (JMSException je) {
            log.severe(je.getMessage());
        } catch (Exception e) {
            log.severe(e.getMessage());
        }
    }
+5
1

, , , . , , . wlfullclient.jar wljmsclient.jar wlsasclient.jar . wljmsclient.jar wlsasclient.jar , . , 30 , 30 , , :

public boolean setReceiver(MessageListener listener) {
        try {
            Properties parm = new Properties();
            parm.setProperty("java.naming.factory.initial",
                    "weblogic.jndi.WLInitialContextFactory");
            parm.setProperty("java.naming.provider.url", URL);
            parm.setProperty("java.naming.security.principal", username);
            parm.setProperty("java.naming.security.credentials", password);
            ctx = new InitialContext(parm);
            final QueueConnectionFactory connectionFactory = (QueueConnectionFactory) ctx
                    .lookup(conFactoryName);
            connection = connectionFactory.createQueueConnection();
            ((WLConnection) connection)
                    .setReconnectPolicy(JMSConstants.RECONNECT_POLICY_ALL);
            ((WLConnection) connection).setReconnectBlockingMillis(30000L);
            ((WLConnection) connection).setTotalReconnectPeriodMillis(-1L);
            session = connection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            queue = (Queue) ctx.lookup(queueName);
            consumer = session.createConsumer(queue);
            consumer.setMessageListener(listener);

            connection.setExceptionListener(new ExceptionListener() {
                @Override
                public void onException(JMSException arg0) {
                    // Assume Disconnected.
                    Logger log2 = new MyLogger().getLogger("BPEL Client");
                    if (arg0 instanceof LostServerException) {
                        log2.severe("Connection to the Server has been lost, will retry in 30 seconds. "
                                + arg0.toString());
                    } else {
                        log2.severe(arg0.toString());
                    }

                }
            });
            connection.start();
            log.info("Successfully connected to " + URL);
            return true;
        } catch (JMSException je) {
            log.severe("Could not connect to the WebLogic Server, will retry in 30 seconds. "
                    + je.getMessage());
            try {
                Thread.sleep(30000L);
            } catch (InterruptedException e) {
                log.warning(e.toString());
            }
            return setReceiver(listener);
        } catch (Exception e) {
            log.severe("Could not connect to the WebLogic Server, will retry in 30 seconds. "
                    + e.toString());
            try {
                Thread.sleep(30000L);
            } catch (InterruptedException ie) {
                log.warning(ie.toString());
            }
            return setReceiver(listener);

        }
    }
+3

All Articles