23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.1.2.3 The TopicConnectionFactory<br />

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

Once a JNDI InitialContext object is instantiated, it can be used to look up the<br />

TopicConnectionFactory in the messaging server's naming service:<br />

TopicConnectionFactory conFactory =<br />

(TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");<br />

The javax.jms.TopicConnectionFactory is used to manufacture connections to a message<br />

server. A TopicConnectionFactory is a type of administered object, which means that its<br />

attributes and behavior are configured by the system administrator responsible for the<br />

messaging server. The TopicConnectionFactory is implemented differently by each<br />

vendor, so configuration options available to system administrators vary from product to<br />

product. A connection factory might, for example, be configured to manufacture<br />

connections that use a particular protocol, security scheme, clustering strategy, etc. A<br />

system administrator might choose to deploy several different TopicConnectionFactory<br />

objects, each configured with its own JNDI lookup name.<br />

The TopicConnectionFactory provides two overloaded versions of the<br />

createTopicConnection( ) method:<br />

package javax.jms;<br />

public interface TopicConnectionFactory extends ConnectionFactory {<br />

public TopicConnection createTopicConnection( )<br />

throws JMSException, JMSSecurityException;<br />

public TopicConnection createTopicConnection(String username,<br />

String password) throws JMSException, JMSSecurityException;<br />

}<br />

These methods are used to create TopicConnection objects. The behavior of the no-arg<br />

method depends on the JMS provider. Some JMS providers will assume that the JMS<br />

client is connecting under anonymous security context, while other providers may assume<br />

that the credentials can be obtained from JNDI or the current thread. [1] The second method<br />

provides the client with a username-password authentication credential, which can be used<br />

to authenticate the connection. In our code, we choose to authenticate the connection<br />

explicitly with a username and password.<br />

[1] Thread-specific storage is used with the <strong>Java</strong> Authentication and Authorization <strong>Service</strong> ( JAAS) to<br />

allow security credentials to transparently propagate between resources and applications.<br />

2.1.2.4 The TopicConnection<br />

The TopicConnection is created by the TopicConnectionFactory :<br />

// Look up a JMS connection factory<br />

TopicConnectionFactory conFactory =<br />

(TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");<br />

// Create a JMS connection<br />

TopicConnection connection =<br />

conFactory.createTopicConnection(username, password);<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!