How does multi threading with JMS and OpenMQ in java works?

240 Views Asked by At

I am newbie in JMS and OpenMQ. I am using both in my code with multi threading but I am getting this weird output when executing the code. Can anyone please explain how this statements in multi threading executes?

public void receiveData(){
    try {
        Hashtable<String, String> contextParams = new Hashtable<>();
        contextParams.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
        contextParams.put(Context.PROVIDER_URL, "file:///C:/jndiStorage");

        Context ctx = new InitialContext(contextParams); 

        QueueReceiver server = new QueueReceiver(ctx, "firstQueueConnectionFactory", "firstQueue");
        server.start();         
        System.out.println("staement 1");

    }catch (NamingException e) {
        System.out.println("JNDI Problem. Check used hostname and/or Glassfish configuration.");
        e.printStackTrace();
    }
}

And my other method is something like this which return list of objects received

public List<Ticket> getAllTickets() throws TicketException {
    receiveData();
    System.out.println("statement 2");
    return JMSLocalTicketStore.entrySet().stream().map(entry -> (Ticket) entry.getValue().clone()).collect(Collectors.toList());


}

QueueReceiver class which is extending Thread class is something like this :

public QueueReceiver(Context ctx, String connFactoryName, String queueName)
        throws NamingException {

    // Look up the connection factory object in the JNDI context provided
    connFactory = (ConnectionFactory) ctx.lookup(connFactoryName);

    // Look up the Destination in the JNDI context
    destination = (Destination) ctx.lookup(queueName);
}

private void startServer() {
    System.out.println("\t [RECEIVER]: Start waiting for messages");
    active = true;
    // Create JMS Context
    try (JMSContext jmsContext = connFactory.createContext()) {
        // Create a JMSConsumer to receive message
        JMSConsumer consumer = jmsContext.createConsumer(destination);
        while (active) {
            Ticket ticket = (Ticket) consumer.receiveBody(Ticket.class, 5000);

            // if no message is received with 5 secs messsage == null
            if (ticket != null) {
                System.out.println("Reveicer statement 1");
                stopServer();

            }else{
                empty();
            }
        }
    }
    System.out.println("\t [RECEIVER]: Stopped.");
}

public void empty(){
    active = false;
    System.out.println("No message in the OpenMQ");
}

public void stopServer() {
    active = false;
    System.out.println("\t [RECEIVER]: Stopping to listen for messages.");
}

@Override
public void run() {
    startServer();
}

}

And my output is :

staement 1
statement 2
 [RECEIVER]: Start waiting for messages
Reveicer statement 1
 [RECEIVER]: Stopping to listen for messages.
 [RECEIVER]: Stopped.

Why it is not printing all the receiver statements before statement 2 of getAllTickets()

0

There are 0 best solutions below