|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
Now that we have a queue connection, the code basically follows the point-to-point model's standard sequence for sending a message: use the queue connection to create a new queue session; use the session to create a new queue sender; and use the sender to send the message to the queue.
Once the writeMessage() method returns, the doAfterBody() method returns the SKIP_BODY constant to inform the Web server that the body has been processed.
Finally, the handler overrides the release() method. The Web server calls release() before reusing a handler class's instance. release() is responsible for cleaning up the handler instance so that the Web server can reuse the handler as a brand-new instance.
Listing 2 shows the complete implementation of the write tag:
Next, let's take a look at the tag handler implementation for the read tag. As this handler is similar to the write tag's handler, I will only discuss the differences.
Since read's handler does not need to manipulate the tag body, it extends the TagSupport class and overrides the doStartTag() method. doStartTag()'s implementation simply calls the private readMessage() method and prints the results to the page as shown below:
pageContext.getOut().print(readMessage());
readMessage() follows the same steps as the writeMessage() method in the write tag handler: it parses the destination and executes some piece of code based on the destination type. For example, if the
destination type is queue, the following code will execute:
String message = null;
QueueConnection connection =
JMSQueueConnectionFactory.getConnection();
try
{
QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(parts[1]);
QueueReceiver receiver = session.createReceiver(queue);
TextMessage msg = (TextMessage)receiver.receive();
message = msg.getText();
receiver.close();
session.close();
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
JMSQueueConnectionFactory.releaseConnection();
}
return(message);
The code above resembles the code executed in the writeMessage() method, with a destination of type queue.
Again, note the calls to the static methods getConnection() and releaseConnection() on the JMSQueueConnectionFactory() class. I will go into the details of those methods in the next section.
After obtaining the queue connection, the code follows the point-to-point messaging model's standard sequence for message
retrieval. After retrieving the message, readMessage() extracts the text String from the message and returns it to the caller -- in this case, the doStartTag() method.
After readMessage() returns, the doStartTag() method returns the SKIP_BODY constant to inform the Web server to skip the tag's body.
Listing 3 shows the complete implementation of the read tag handler.
Add a healthy dose of efficiency
Now let's look at the mysterious JMSQueueConnectionFactory class, which I've avoided so far. Two factors encouraged me to create that class: