2

Below is the Java code to consume the durable subscription

private void execute()throws Exception {
    logger.debug("Creating JNDI context");

    Properties jndiProps = new Properties();

    jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, config.getJndiFactory());
    jndiProps.setProperty(Context.PROVIDER_URL, config.getJndiProviderUrl());
    jndiProps.setProperty(Context.SECURITY_CREDENTIALS, config.getJndiSecurityCredential());
    jndiProps.setProperty(Context.SECURITY_PRINCIPAL, config.getJndiSecurityPrincipal());

    logger.debug("JNDI properties: " + jndiProps.toString());

    jndiContext = new InitialContext(jndiProps);
    logger.debug("JNDI context created successfully");

    logger.debug("Looking up ConnectionFactory : " + config.getJmsConnFactory());
    TopicConnectionFactory connFactory = (TopicConnectionFactory) jndiContext.lookup(config.getJmsConnFactory());

    logger.debug("Creating connection object");
    conn = connFactory.createTopicConnection(config.getJmsUserName(), config.getJmsPasswd());
    logger.debug("Connection object successfully created");



    TopicSession session = null;
    MessageConsumer subscriber = null;
    try {
        session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        logger.debug("Starting connection");
        conn.start();
        logger.debug("Getting destinaton");
        Topic topic = (Topic)jndiContext.lookup(config.getDestination());
        if (topic == null) {
            throw new RuntimeException("Invalid destination");
        }
        logger.debug("Creating durable subscriber");
        if(config.isDurable()){
            subscriber = session.createDurableSubscriber(topic, config.getSubscriberId());
        }else{
            subscriber = session.createSubscriber(topic);
        }

        boolean runFlag = true;
        do {
            logger.debug("Receiving messages...");
            TextMessage message = (TextMessage) subscriber.receive(DEF_TIMEOUT_MILLIS);
            if (message != null) {
                logger.debug("Received message : " + message.getText());
                continue;
            }
            logger.debug("No available messages now");
            runFlag = false;
        } while (runFlag);
        logger.debug("There is no more messages available. Exiting...");
    } finally {
        if(config.isDurable()){
            session.unsubscribe(config.getSubscriberId());
        }
        close(subscriber);
        close(session);
    }
}

While executing I am getting the "TCPLink Error: invalid magic in the message". And after that, session and connection is getting terminated automatically.

javax.jms.IllegalStateException: Session is closed

javax.jms.JMSException: Connection has been terminated

Please help.

Thanks in advance

Rajiv Kumar
  • 194
  • 9

0 Answers0