Using the JMS 2.0 simplified API and injection
to send a message (JavaEESenderNewCDI)

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

@Stateless
@LocalBean
public class JavaEESenderNewCDI {

    @Inject private JMSContext context;
    
    @Resource(lookup = "java:global/jms/demoQueue")
    Queue inboundQueue;
               
    public void sendMessageNewCDI(String payload) {
        try {
            context.createProducer().send(inboundQueue, payload);
        } catch (JMSRuntimeException ex){
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
}

This example shows:

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

Now examine using the JMS 1.1-style API to receive a message

Run this example

JMS 2.0 examples home