Using the JMS 2.0 simplified API
to send a message (JavaEESenderNew)

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

@Stateless
@LocalBean
public class JavaEESenderNew {

    @Resource(lookup = "java:global/jms/demoConnectionFactory")
    ConnectionFactory connectionFactory;
    
    @Resource(lookup = "java:global/jms/demoQueue")
    Queue demoQueue;
    
    public void sendMessageNew(String payload) {
        try {
            JMSContext context = connectionFactory.createContext();
            try {
                context.createProducer().send(demoQueue, payload);
            } finally {
                context.close();
            }
        } catch (JMSRuntimeException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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 do the same thing

 Run this example

JMS 2.0 examples home