2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.internal.client.jms;
22
23 import java.util.Hashtable;
24 import javax.jms.QueueConnection;
25 import javax.jms.JMSException;
26 import javax.jms.QueueSession;
27 import javax.jms.Session;
28 import javax.jms.QueueConnectionFactory;
29 import javax.jms.Queue;
30 import javax.jms.QueueSender;
31 import javax.naming.InitialContext;
32 import javax.naming.Context;
33 import javax.naming.NamingException;
34
35 import org.apache.cactus.util.JmsConfiguration;
36 import org.apache.cactus.util.ChainedRuntimeException;
37
38
39
40
41
42
43
44
45
46
47 public class JmsClientHelper
48 {
49
50
51
52 private static QueueSession queueSession;
53
54
55
56
57
58
59
60 protected static QueueConnection createQueueConnection()
61 throws JMSException
62 {
63 QueueConnection queueConnection =
64 getQueueConnnectionFactory().createQueueConnection();
65 return queueConnection;
66 }
67
68
69
70
71
72
73
74
75 public static synchronized QueueSession getQueueSession()
76 {
77 if (queueSession == null) {
78 try
79 {
80 queueSession =
81 createQueueConnection().createQueueSession(false,
82 Session.AUTO_ACKNOWLEDGE);
83 }
84 catch (JMSException e)
85 {
86 throw new ChainedRuntimeException(
87 "Failed to create JMS Queue Session", e);
88 }
89 }
90 return queueSession;
91 }
92
93
94
95
96
97 public static InitialContext getInitialContext()
98 {
99 InitialContext context = null;
100 try
101 {
102 Hashtable env = new Hashtable();
103 env.put(Context.INITIAL_CONTEXT_FACTORY,
104 JmsConfiguration.getJndiInitialContextFactory());
105 env.put(Context.PROVIDER_URL,
106 JmsConfiguration.getJndiProviderURL());
107 env.put(Context.URL_PKG_PREFIXES,
108 JmsConfiguration.getJndiUrlPkgPrefixes());
109 env.put("j2ee.clientName", "JmsClientHelper");
110
111 context = new InitialContext(env);
112 System.out.println(context);
113 }
114 catch (NamingException e)
115 {
116 throw new ChainedRuntimeException(
117 "Failed to create JNDI initial context", e);
118 }
119
120 return context;
121 }
122
123
124
125
126 public static QueueConnectionFactory getQueueConnnectionFactory()
127 {
128 QueueConnectionFactory queueConnectionFactory = null;
129 try
130 {
131 queueConnectionFactory =
132 (QueueConnectionFactory) (getInitialContext().
133 lookup(JmsConfiguration.getJmsConnectionFactoryJndiName()));
134 }
135 catch (NamingException e)
136 {
137 throw new ChainedRuntimeException(
138 "Failed to lookup [" +
139 JmsConfiguration.getJmsConnectionFactoryJndiName() +
140 "] Connection Factory in JNDI", e);
141 }
142
143 return queueConnectionFactory;
144 }
145
146
147
148
149
150
151
152 public static Queue getQueue(String theQueueName)
153 {
154 Queue queue = null;
155 try
156 {
157 queue = (Queue) (getInitialContext().lookup(theQueueName));
158 }
159 catch (NamingException e)
160 {
161 throw new ChainedRuntimeException(
162 "Failed to lookup [" + theQueueName + "] Queue in JNDI", e);
163 }
164 return queue;
165 }
166
167
168
169
170
171
172
173 public static QueueSender createQueueSender(String theQueueName)
174 {
175 Queue queue = getQueue(theQueueName);
176 QueueSession queueSession = getQueueSession();
177
178 QueueSender queueSender = null;
179 try
180 {
181 queueSender = queueSession.createSender(queue);
182 }
183 catch (JMSException e)
184 {
185 throw new ChainedRuntimeException("Failed to create queue sender" +
186 "for queue [" + queue + "]", e);
187 }
188
189 return queueSender;
190 }
191 }