Sending messages to a message queue

The following example illustrates how to add messages to a message queue:

package net.ensode.javaee8book.jmsptpproducer; 
 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.annotation.Resource; 
import javax.enterprise.context.RequestScoped; 
import javax.inject.Named; 
import javax.jms.ConnectionFactory; 
import javax.jms.JMSContext; 
import javax.jms.JMSProducer; 
import javax.jms.Queue; 
 
@Named 
@RequestScoped 
public class MessageSender { 
 
    @Resource    
private ConnectionFactory connectionFactory;

@Resource(mappedName = "jms/JavaEE8BookQueue")

private Queue queue;
private static final Logger LOG = Logger.getLogger(MessageSender.class.getName()); public void produceMessages() { JMSContext jmsContext = connectionFactory.createContext(); JMSProducer jmsProducer = jmsContext.createProducer(); String msg1 = "Testing, 1, 2, 3. Can you hear me?"; String msg2 = "Do you copy?"; String msg3 = "Good bye!"; LOG.log(Level.INFO, "Sending the following message: {0}",
msg1); jmsProducer.send(queue, msg1); LOG.log(Level.INFO, "Sending the following message: {0}",
msg2); jmsProducer.send(queue, msg2); LOG.log(Level.INFO, "Sending the following message: {0}",
msg3); jmsProducer.send(queue, msg3); } }

The produceMessages() method in the MessageSender class performs all the necessary steps to send messages to a message queue.

The first thing this method does is create an instance of javax.jms.JMSContext by invoking the createContext() method on the injected instance of javax.jms.ConnectionFactory. Notice that the mappedName attribute of the @Resource annotation decorating the connection factory object matches the JNDI name of the connection factory we set up in the GlassFish web console. Behind the scenes, a JNDI lookup is made using this name to obtain the connection factory object.

Next, we create an instance of javax.jms.JMSProducer by invoking the createProducer() method on the JMSContext instance we just created.

After obtaining an instance of JMSProducer, the code sends a series of text messages by invoking its send() method. This method takes the message destination as its first parameter, and a String containing the message text as its second parameter.

There are several overloaded versions of the send() method in JMSProducer. The one we used in our example is a convenience method that creates an instance of javax.jms.TextMessage and sets its text to the String we provide as the second parameter in the method invocation.

Although the above example sends only text messages to the queue, we are not limited to this type of message. The JMS API provides several types of messages that can be sent and received by JMS applications. All message types are defined as interfaces in the javax.jms package.

The following table lists all of the available message types:

Message Type

Description

BytesMessage

Allows sending an array of bytes as a message. JMSProducer has a convenience send() method that takes an array of bytes as one of its parameters. This method creates an instance of javax.jms.BytesMessage on the fly as the message is being sent.

MapMessage

Allows sending an implementation of java.util.Map as a message. JMSProducer has a convenience send() method that takes Map as one of its parameters. This method creates an instance of javax.jms.MapMessage on the fly as the message is being sent.

ObjectMessage

Allows sending any Java object implementing java.io.Serializable as a message. JMSProducer has a convenience send() method that takes an instance of a class implementing java.io.Serializable as its second parameter. This method creates an instance of javax.jms.ObjectMessage on the fly as the message is being sent.

StreamMessage

Allows sending an array of bytes as a message. Differs from BytesMessage in that it stores the type of each primitive type added to the stream.

TextMessage

Allows sending a java.lang.String as a message. As seen in their above example, JMSProducer has a convenience send() method that takes a String as its second parameter, this method creates an instance of javax.jms.TextMessage on the fly as the message is being sent.

For more information on all of the above message types, consult the JavaDoc documentation at https://javaee.github.io/javaee-spec/javadocs/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.158.165