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 body) { try (JMSContext context = connectionFactory.createContext();){ context.createProducer().send(demoQueue, body); } 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