Using the JMS 2.0 simplified API with injection
to send a message,
setting delivery options and message properties (JavaEESenderNewCDIWithProperties)

Here is a simple Java EE session bean which extends the earlier example to demonstrate how to use the JMS 2.0 simplified API and an injected JMSContext to send a message whilst setting message delivery options and message properties.

@Stateless
@LocalBean
public class JavaEESenderNewCDIWithProperties {

    @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 void sendMessageNewCDIWithProperties(String payload) {
        try {
            context.createProducer().setPriority(1).setProperty("foo", "987654").send(inboundQueue, payload);
        } catch (JMSRuntimeException ex) {
            Logger.getLogger(JavaEESenderOldWithProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

This example additionally shows:

Injection of JMSContext is only available in the Java EE web or EJB container

API for setting delivery and message options on a non-injected JMSContext is identical (not shown)

 Finally examine the JMS 2.0 simplified API and injection to receive a message, displaying delivery options and message properties

 Run this example

JMS 2.0 examples home