View Javadoc

1   package org.lsst.ccs.bus.jms;
2   
3   import org.lsst.ccs.utilities.logging.Logger;
4   
5   import javax.jms.Connection;
6   import javax.jms.ConnectionFactory;
7   import javax.jms.Destination;
8   import javax.jms.JMSException;
9   import javax.jms.MessageProducer;
10  import javax.jms.Session;
11  import javax.naming.Context;
12  import javax.naming.InitialContext;
13  import javax.naming.NamingException;
14  import java.util.Properties;
15  import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
16  
17  /**
18   * JMS Queue session factory
19   * 
20   * @author aubourg
21   * 
22   */
23  
24  public class QueueSessionFactory {
25  	static protected QueueSessionFactory instance = null;
26  
27  	public static synchronized QueueSessionFactory getSessionFactory() {
28  		if (instance == null) {
29  			instance = new QueueSessionFactory();
30  		}
31  		return instance;
32  	}
33  
34  	static Logger log = Logger.getLogger("org.lsst.ccs.bus.jms.QueueSessionFactory");
35  
36  	Connection conn;
37  
38  	Context context;
39  
40  	ConnectionFactory cf;
41  
42  	public Session getQueueSession() {
43  		try {
44  			if (context == null) {
45                              Properties props = BootstrapResourceUtils.getBootstrapProperties("jndi");
46                              context = new InitialContext(props);
47  			}
48  			if (cf == null) {
49  				cf = (ConnectionFactory) context.lookup("ConnectionFactory");
50  			}
51  			if (conn == null) {
52  				conn = cf.createConnection();
53  				conn.start();
54  			}
55  
56  			Session session = conn.createSession(false,
57  					Session.AUTO_ACKNOWLEDGE);
58  
59  			return session;
60  		} catch (NamingException e1) {
61  			e1.printStackTrace();
62  			throw new RuntimeException(e1);
63  		} catch (JMSException e2) {
64  			e2.printStackTrace();
65  			throw new RuntimeException(e2);
66  		}
67  	}
68  
69  	public MessageProducer getCommandReplySender(Session sess, Destination dest) {
70  		try {
71  			return sess.createProducer(dest);
72  		} catch (JMSException e) {
73  			e.printStackTrace();
74  			throw new RuntimeException(e);
75  		}
76  	}
77  
78  }