0

I am following this tutorial to read messages from a given subscription. I have created a Receive class like this -

public class Receive implements MessageListener{

    private static boolean runReceiver = true;
    private Connection connection;
    private Session sendSession;
    private Session receiveSession;
    private MessageProducer sender;
    private MessageConsumer receiver;
    private static Random randomGenerator = new Random();

    public Receive() throws Exception {
        Hashtable<String, String> env = new Hashtable<String, String>();

        env.put("connectionfactory.SBCF", "amqps://All:[shared-access-key]@[namespace].servicebus.windows.net?amqp.idleTimeout=120000");
        env.put("topic.TOPIC", "job/Subscriptions/job-test-subscription");

        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");

        Context context = new InitialContext(env);

        // Look up ConnectionFactory and Queue
        ConnectionFactory cf = (ConnectionFactory) context.lookup("SBCF");
        Destination queue = (Destination) context.lookup("TOPIC");

        // Create Connection
        connection = cf.createConnection();

        // Create sender-side Session and MessageProducer
        sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        sender = sendSession.createProducer(queue);

        if (runReceiver) {
            // Create receiver-side Session, MessageConsumer,and MessageListener
            receiveSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            receiver = receiveSession.createConsumer(queue);
            receiver.setMessageListener(this);
            connection.start();
        }
    }
    public void close() throws JMSException {
        connection.close();
    }

    public void onMessage(Message message) {
        try {
            System.out.println("Received message with JMSMessageID = " + message.getJMSMessageID());
            message.acknowledge();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Receive simpleReceiver = new Receive();
    } catch (Exception e) {
        e.printStackTrace();
    }
}   

On executing this programI am getting this error -

    log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
javax.jms.JMSException: hostname can't be null
    at org.apache.qpid.jms.exceptions.JmsExceptionSupport.create(JmsExceptionSupport.java:86)
    at org.apache.qpid.jms.exceptions.JmsExceptionSupport.create(JmsExceptionSupport.java:108)
    at org.apache.qpid.jms.JmsConnection.connect(JmsConnection.java:172)
    at org.apache.qpid.jms.JmsConnectionFactory.createConnection(JmsConnectionFactory.java:204)
    at org.apache.qpid.jms.JmsConnectionFactory.createConnection(JmsConnectionFactory.java:191)
    at Receive.<init>(Receive.java:41)
    at Receive.main(Receive.java:63)
Caused by: java.io.IOException: hostname can't be null
    at org.apache.qpid.jms.util.IOExceptionSupport.create(IOExceptionSupport.java:45)
    at org.apache.qpid.jms.provider.amqp.AmqpProvider$2.run(AmqpProvider.java:217)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: hostname can't be null
    at java.net.InetSocketAddress.checkHost(InetSocketAddress.java:149)
    at java.net.InetSocketAddress.createUnresolved(InetSocketAddress.java:254)
    at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:126)
    at org.apache.qpid.jms.transports.netty.NettyTcpTransport.connect(NettyTcpTransport.java:167)
    at org.apache.qpid.jms.provider.amqp.AmqpProvider$2.run(AmqpProvider.java:195)
    ... 7 more

Error is pointing towards this line - connection = cf.createConnection(); Also would like to know if providing my topic and subscription name like this -job/Subscriptions/job-test-subscription is fine or not.

Apologies for posting a long post.

Anurag Sharma
  • 4,839
  • 13
  • 59
  • 101

1 Answers1

0

According to the subsection Service Bus entity address from here, the job/Subscriptions/job-test-subscription value is your subscription address, not topic. Please change your url and topic property as below and try again.

enter image description here

connectionfactory.SBCF=amqps://All:[shared-access-key]@[namespace].servicebus.windows.net/job/Subscriptions/job-test-subscription?amqp.idleTimeout=120000
topic.TOPIC=job

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43