Implementing the service-activator pattern
We are now going to show code examples of the three solutions offered by the Java EE platform.
Implementing sending and receiving messages with JMS
The following is an example of a JMS message sender. This is a CDI bean that is responsible for sending messages:
public class MessageSender { @Inject @JMSConnectionFactory("jms/connectionFactory") JMSContext context; @Resource(mappedName = "jms/myQueue") Destination queue; public void sendSomeMessage (String message) { context.createProducer().send(queue, message); } }
The @JMSConnectionFactory
annotation indicates which ConnectionFactory
should be used to create the JMSContext
. The following code block shows an MDB that receives the message generated by the producer described earlier:
@MessageDriven( activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class EmailService implements MessageListener ...