KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > jobs > ee > jms > SendTopicMessageJob


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.jobs.ee.jms;
22
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicConnectionFactory JavaDoc;
29 import javax.jms.TopicPublisher JavaDoc;
30 import javax.jms.TopicSession JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33
34 import org.quartz.Job;
35 import org.quartz.JobDataMap;
36 import org.quartz.JobDetail;
37 import org.quartz.JobExecutionContext;
38 import org.quartz.JobExecutionException;
39
40 /**
41 * <p>
42 * A <code>Job</code> that sends a <code>javax.jms.Message</code> to a
43 * <code>javax.jms.Topic</code>.
44 *
45 * <p>
46 * The following properties are expected to be provided in the <code>JobDataMap</code>:
47 *
48 * <ul>
49 * <li><code>JMS_CONNECTION_FACTORY_JNDI</code> - The JNDI name of the JMS Connection Factory.</li>
50 * <li><code>JMS_DESTINATION_JNDI</code> - The JNDI name of the JMS destination.</li>
51 * <li><code>JMS_USE_TXN</code> - Whether or not to use a transacted <code>javax.jms.Session</code>.</li>
52 * <li><code>JMS_ACK_MODE</code> - The acknowledgement mode for the <code>javax.jms.Session</code>.</li>
53 * <li><code>JMS_MSG_FACTORY_CLASS_NAME</code> - The implementation class name for the <code>JmsMessageFactory</code>.</li>
54 * </ul>
55 *
56 * <p>
57 * The following properties are optional
58 *
59 * <ul>
60 * <li><code>JMS_USER</code> - The JMS user for secure destinations.
61 * <li><code>JMS_PASSWORD</code> - The JMS password for secure destinations.
62 * </ul>
63 *
64 * <p>
65 * The following properties can be used for JNDI support:
66 * <ul>
67 * <li><code>INITIAL_CONTEXT_FACTORY</code> - The java.naming.factory.initial setting for JNDI.
68 * <li><code>PROVIDER_URL</code> - The java.naming.provider.url for JNDI.
69 * </ul>
70 *
71 *
72 * @see JmsMessageFactory
73 *
74 * @author Weston M. Price
75 *
76 *
77 */

78 public class SendTopicMessageJob implements Job {
79
80     public void execute(JobExecutionContext context)
81         throws JobExecutionException {
82
83         TopicConnectionFactory JavaDoc tcf = null;
84         TopicConnection JavaDoc connection = null;
85         TopicSession JavaDoc session = null;
86         Topic JavaDoc topic = null;
87         TopicPublisher JavaDoc publisher = null;
88         InitialContext JavaDoc ctx = null;
89         String JavaDoc user = null;
90         String JavaDoc pw = null;
91
92         final JobDetail detail = context.getJobDetail();
93         final JobDataMap jobDataMap = detail.getJobDataMap();
94
95         try {
96
97             ctx = JmsHelper.getInitialContext(jobDataMap);
98
99             tcf = (TopicConnectionFactory JavaDoc) ctx
100                     .lookup(JmsHelper.JMS_CONNECTION_FACTORY_JNDI);
101
102             if (JmsHelper.isDestinationSecure(jobDataMap)) {
103
104                 user = jobDataMap.getString(JmsHelper.JMS_USER);
105                 pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD);
106                 connection = tcf.createTopicConnection(user, pw);
107
108             } else {
109     
110                 connection = tcf.createTopicConnection();
111
112             }
113
114             session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
115             topic = (Topic JavaDoc) ctx.lookup(JmsHelper.JMS_DESTINATION_JNDI);
116             publisher = session.createPublisher(topic);
117             String JavaDoc factoryClassName = jobDataMap.getString(JmsHelper.JMS_MSG_FACTORY_CLASS_NAME);
118             JmsMessageFactory factory = JmsHelper.getMessageFactory(factoryClassName);
119             Message JavaDoc m = factory.createMessage(jobDataMap, session);
120             publisher.publish(m);
121
122         } catch (NamingException JavaDoc e) {
123
124             throw new JobExecutionException(e);
125
126         } catch (JMSException JavaDoc e) {
127
128             throw new JobExecutionException(e);
129
130         } catch (JmsJobException e) {
131
132             throw new JobExecutionException(e);
133
134         } finally {
135
136             JmsHelper.closeResource(publisher);
137             JmsHelper.closeResource(session);
138             JmsHelper.closeResource(connection);
139
140         }
141
142     }
143
144 }
145
Popular Tags