Using the JMS 2.0 simplified API and injection
to receive a message (JavaEESyncReceiverNewCDI)

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

@Stateless
@LocalBean
public class JavaEESyncReceiverNewCDI {

    @Inject
    @JMSConnectionFactory("java:global/jms/demoConnectionFactory") // <== could omit this and use the default
    private JMSContext context;

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

    public String receiveMessageNewCDI() {
        try {
            JMSConsumer consumer = context.createConsumer(inboundQueue);
            return "Received " + consumer.receivePayload(String.class, 1000);
        } catch (JMSRuntimeException ex) {
            Logger.getLogger(JavaEESyncReceiverOld.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}

This example shows:

Note that injection of JMSContext is only available in the Java EE web or EJB container

You can stop here, or carry on and examine using the JMS 1.1-style API to send a message, setting delivery options and message properties

 Run this example

JMS 2.0 examples home