Using the JMS 2.0 simplified API
to receive a message (JavaEESyncReceiverNew)

Here is a simple Java EE session bean which demonstrates how to use the JMS 2.0 simplified API to synchronously receive a message.

@Stateless
@LocalBean
public class JavaEESyncReceiverNew {

    @Resource(lookup = "java:global/jms/demoConnectionFactory")
    ConnectionFactory connectionFactory;

    @Resource(lookup = "java:global/jms/demoQueue")
    Queue demoQueue;

    public String receiveMessageNew() {
        try {
            JMSContext context = connectionFactory.createContext();
            try {
                JMSConsumer consumer = context.createConsumer(demoQueue);
                return "Received " + consumer.receivePayload(String.class, 1000);
            } finally {
                context.close();
            }
        } catch (JMSRuntimeException ex) {
            Logger.getLogger(JavaEESyncReceiverOld.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

This example shows:

This example is for Java EE but the API for Java SE is similar

 Now compare this with using the JMS 2.0 simplified API and injection to do the same thing

 Run this example

JMS 2.0 examples home