crecode.com

Receiving Messages from a Message Topic in Java Generating Code39 in Java Receiving Messages from a Message Topic




How to generate, print barcode using .NET, Java sdk library control with example project source code free download:
7. using none tobuild none on asp.net web,windows application QR Code ISO speicification After obtaini none none ng a JMS connection and a JMS session, we pass the Topic object to the createProducer() method in the Session object. This method returns an instance of javax.jms.

MessageProducer that we can use to send messages to the JMS topic.. Receiving Messages from a Message Topic Just as sendi none none ng messages to a Message Topic is nearly identical to sending messages to a Message Queue, receiving messages from a Message Topic is nearly identical to receiving messages from a Message Queue.. package net.e nsode.glassfishbook; import import import import import import import import javax.

annotation.Resource; javax.jms.

Connection; javax.jms.ConnectionFactory; javax.

jms.JMSException; javax.jms.

MessageConsumer; javax.jms.Session; javax.

jms.TextMessage; javax.jms.

Topic;. public class none for none MessageReceiver { @Resource(mappedName = "jms/GlassFishBookConnectionFactory") private static ConnectionFactory connectionFactory; @Resource(mappedName = "jms/GlassFishBookTopic") private static Topic topic; public void getMessages() { Connection connection; MessageConsumer messageConsumer; TextMessage textMessage; boolean goodByeReceived = false; try { connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.

AUTO_ACKNOWLEDGE); messageConsumer = session.createConsumer(topic); connection.start(); while (!goodByeReceived) { System.

out.println("Waiting for messages..

."); textMessage = (TextMessage) messageConsumer.receive(); [ 241 ].

Java Messagin g Service if (textMessage != null) { System.out.print("Received the following message: "); System.

out.println(textMessage.getText()); System.

out.println(); } if (textMessage.getText() != null && textMessage.

getText().equals("Good bye!")) { goodByeReceived = true; } } messageConsumer.close(); session.

close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } public static void main(String[] args) { new MessageReceiver().

getMessages(); } }. Once again th none none e differences between this code and the corresponding code for PTP are trivial. Instead of declaring an instance of a class implementing javax.jms.

Queue, we declare a class implementing javax.jms.Topic; we use the @Resource annotation to inject an instance of this class into our code, using the JNDI name we used when creating it in the GlassFish web console.

After obtaining a JMS connection and session, we pass the Topic object to the createConsumer() method in the Session object. This method returns an instance of javax.jms.

MessageConsumer that we can use to receive messages from the JMS topic. Using the pub/sub messaging domain, as illustrated in this section, has the advantage that messages can be sent to several message consumers. We can easily test this by concurrently executing two instances of the MessageReceiver class we developed in this section, then executing the MessageSender class we developed in the previous section.

We should see console output for each instance, indicating that both instances received all messages.. [ 242 ]. 7 . Just as with message queues, messages can be retrieved asynchronously from a message Topic. The procedure to do so is so similar to the message queue version that we will not show an example. To convert the asynchronous example shown earlier in this chapter to use a message topic, simply replace the javax.

jms.Queue variable with an instance of javax.jms.

Topic and inject the appropriate instance by using "jms/GlassFishBookTopic" as the value of the mappedName attribute of the @Resource annotation decorating the instance of javax.jms.Topic.

. Creating Durable Subscribers The disadvant age of using the pub/sub messaging domain is that message consumers must be executing when the messages are sent to the topic. If the message consumer is not executing at the time, it will not receive the messages, whereas in PTP, messages are kept in the queue until the message consumer executes. Fortunately, the JMS API provides a way to use the pub/sub messaging domain and keep messages in the topic until all subscribed message consumers execute and receive the messages.

This can be accomplished by creating durable subscribers to a JMS Topic. In order to be able to service durable subscribers, we need to set the ClientId property of our JMS connection factory. Each durable subscriber must have a unique client ID, therefore a unique connection factory must be declared for each potential durable subscriber.

. InvalidClient none for none IdException Only one JMS client can connect to a Topic for a specific client ID, if more than one JMS client attempts to obtain a JMS connection using the same connection factory, a JMSException stating that the Client ID is already in use will be thrown. The solution is to create a connection factory for each potential client that will be receiving messages from the durable Topic..

As we mention ed before, the easiest way to add a connection factory is through the GlassFish web console. Recall that to add a JMS connection factory through the GlassFish web console, we need to expand the Resources node on the left-hand side, then expand the JMS Resources node, click on the Connection Factories node, and then click on the New..

. button in the main area of the page..

[ 243 ].
Copyright © crecode.com . All rights reserved.