Saturday, July 25, 2009

Key Interfaces in EMS - ConnectionFactory, Connection and Session

ConnectionFactory:



The ConnectionFactory object encapsulates a set of connection configuration parameters. When a client starts, it typically performs a Java Naming and Directory Interface (JNDI) lookup for the ConnectionFactories that it needs. For example, the following code retrieves the InitialContext using the JNDI properties specified by env, then looks up a ConnectionFactory named myConnectionFactory.



Context ctx = new InitialContext(env);
ConnectionFactory myConnectionFactory =
(ConnectionFactory) ctx.lookup("myConnectionFactory");



Connection:



A Connection object encapsulates a virtual connection with the server.
ConnectionFactory objects create Connection objects. You use a Connection to create one or more Session objects. For example, the following creates a Connection:

Connection myConnection =
myConnectionFactory.createConnection()



When a client application completes, all open connections must be closed. Unused open connections are eventually closed, but they do consume resources that could be used for other applications. Closing a connection also closes any Sessions created by the Connection. To close a connection, use the close() method. For example:


myConnection.close();



Session:
A Session is a single-threaded context for producing or consuming messages. You create MessageProducers or MessageConsumers using Session objects. For example, using the myConnection object created in Connection above, the following creates a Session:


Session mySession =

myConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);


The first parameter to the CreateSession() method determines whether the Session is transactional or not. The second parameter specifies the acknowledge mode of messages received by the session.



The remaining Key Interfaces will be explained in tomorrow's posting.

0 comments:

Popular Posts

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP