java-ee Java Messaging Service (JMS) Creating ConnectionFactory


Connection factories are the managed objects that allows application to connect to provider by creating Connection object. javax.jms.ConnectionFactory is an interface that encapsulates configuration parameters defined by an administrator.

For using ConnectionFactory client must execute JNDI lookup (or use injection). The following code gets JNDI InitialContext object and uses it to lookup for ConnectionFactory object under JNDI name:

Context ctx = new InitialContext();
ConnectionFactory connectionFactory = 
                    (ConnectionFactory) ctx.lookup("jms/javaee7/ConnectionFactory");

The methods available in this interface are createConnection() methods that return a Connection object and new JMS 2.0 createContext() methods that return a JMSContext.

It's possible to create a Connection or a JMSContext either with the default user identity or by specifying a username and password:

public interface ConnectionFactory {
    Connection createConnection() throws JMSException;
    Connection createConnection(String userName, String password) throws JMSException;

    JMSContext createContext();
    JMSContext createContext(String userName, String password);
    JMSContext createContext(String userName, String password, int sessionMode);
    JMSContext createContext(int sessionMode);