On 03/23/2016 08:56 AM, Aleks Balaban wrote:
Tim,

Thx, good to know that the failover works! Obviously I've been doing something wrong. Well, as you requested here is my JMS/AMQP client source (I have removed the SSL initialization part used for amqps conncetion ):



Your code relies on there being at least one non-daemon thread in existence at all time to prevent the main app from shutting down which will not always be the case as the code is currently written. Because of this your shutdown hook is being triggered and the shutdown of the connection and its underlying resources is occurring leading to the logs that you are seeing.

Connection URI:
connectionfactory.nm = failover:(amqps://somedomain.com:5671)?failover.reconnectDelay=2000&failover.warnAfterReconnectAttempts=1


Source code:

public class TestClient implements Runnable {

private static final Logger logger = Logger.getLogger(TestClient.class.getName());
    private static Connection connection;

    public static void main(String[] args) {

org.apache.log4j.PropertyConfigurator.configure("./log4j.properties");

        initSSL();

        new Thread(new TestClient()).start();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {

            logger.info("Shutting down...");

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (JMSException e) {
                logger.warning(e.getMessage());
            }
        }));
    }

    @Override
    public void run() {

        try {
            Properties properties = new Properties();

            if (System.getProperty("nmclient.configuration") == null) {
File jarPath = new File(TestClient.class.getProtectionDomain().getCodeSource().getLocation().getPath());
                String propertiesPath = jarPath.getParent();
properties.load(new FileInputStream(propertiesPath + "/config.properties"));
            } else {
properties.load(new FileInputStream(System.getProperty("nmclient.configuration")));
            }

            Context context = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("nm");

            connection = connectionFactory.createConnection();

            connection.setExceptionListener((final JMSException e) -> {
                logger.log(Level.SEVERE, null, e);
            });

Session consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            Queue queueEaup = (Queue) context.lookup("eaup");
MessageConsumer messageConsumer = consumerSession.createConsumer(queueEaup);

messageConsumer.setMessageListener((final Message message) -> {
                try {
                    if (message instanceof TextMessage) {

                        TextMessage payload = (TextMessage) message;
logger.info(payload.getText());
                    }
                    message.acknowledge();
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                }
            });

            connection.start();

        } catch (IOException | NamingException | JMSException e) {
            logger.log(Level.SEVERE, null, e);
        }
    }

    private static void initSSL() {
          // ....
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]




--
Tim Bish
twitter: @tabish121
blog: http://timbish.blogspot.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to