Here is a simple Java EE session bean which extends the earlier example to demonstrate how to use the JMS 1.1 API to send a message whilst setting message delivery options and message properties.
@Stateless @LocalBean public class JavaEESenderOldWithProperties { @Resource(lookup = "java:global/jms/demoConnectionFactory") ConnectionFactory connectionFactory; @Resource(lookup = "java:global/jms/demoQueue") Queue demoQueue; public void sendMessageOldWithProperties(String payload) { try { Connection connection = connectionFactory.createConnection(); try { Session session = connection.createSession(); MessageProducer messageProducer = session.createProducer(demoQueue); messageProducer.setPriority(1); TextMessage textMessage = session.createTextMessage(payload); textMessage.setStringProperty("foo", "bar"); messageProducer.send(textMessage); } finally { connection.close(); } } catch (JMSException ex) { Logger.getLogger(JavaEESenderOldWithProperties.class.getName()).log(Level.SEVERE, null, ex); } } }
This example additionally 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 with injection to do the same thing