Information Technology Reference
In-Depth Information
public class DateClient {
private Queue destination;
private Session session;
private MessageProducer producer;
private MessageConsumer consumer;
private Queue replyQueue;
public static void main(String[] args) throws JMSException, NamingException {
DateClient d = new DateClient();
d.initialize();
System.out.println( "Server's Date: " + d.getDate());
System.exit(0);
}
private String getDate() throws JMSException {
TextMessage requestMessage = session.createTextMessage();
requestMessage.setText(new Date().toString());
requestMessage.setJMSReplyTo(replyQueue);
producer.send(requestMessage);
Message message = (TextMessage) consumer.receive();
if (message instanceof TextMessage)
return ((TextMessage) message).getText();
return "Invalid Message type Received" ;
}
private void initialize() throws JMSException, NamingException {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory" );
props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616" );
props.setProperty( "queue.destination" , "TEST" );
Context ctx = new InitialContext(props);
QueueConnectionFactory connectionFactory =
(QueueConnectionFactory) ctx.lookup( "ConnectionFactory" );
QueueConnection c = connectionFactory.createQueueConnection();
destination = (Queue) ctx.lookup( "destination" );
session = c.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
replyQueue = session.createTemporaryQueue();
consumer = session.createConsumer(replyQueue);
c.start();
}
}
Figure 2.17. JMS client example. The shaded areas illustrate
where usage of the JMS framework is required.
ing in the JMS examples, the JMS framework is highly intrusive, requiring a great
deal of setup and recovery code.
2.6.2
Publish/Subscribe
In contrast to the synchronous models of communication described earlier, pub-
lish/subscribe systems provide a loosely-coupled interaction style where publish-
Search WWH ::




Custom Search