KickJava   Java API By Example, From Geeks To Geeks.

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


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

77 public class SendQueueMessageJob implements Job {
78
79     public void execute(JobExecutionContext context)
80         throws JobExecutionException {
81     
82         
83         QueueConnectionFactory JavaDoc qcf = null;
84         QueueConnection JavaDoc conn = null;
85         QueueSession JavaDoc session = null;
86         Queue JavaDoc queue = null;
87         QueueSender JavaDoc sender = null;
88         InitialContext JavaDoc ctx = null;
89         
90         final JobDetail detail = context.getJobDetail();
91         final JobDataMap jobDataMap = detail.getJobDataMap();
92         
93         try {
94         
95             ctx = JmsHelper.getInitialContext(jobDataMap);
96             
97             if(JmsHelper.isDestinationSecure(jobDataMap)) {
98                 String JavaDoc user = jobDataMap.getString(JmsHelper.JMS_USER);
99                 String JavaDoc pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD);
100                 conn = qcf.createQueueConnection(user, pw);
101             } else {
102                 conn = qcf.createQueueConnection();
103             }
104             
105             boolean useTransactions = JmsHelper.useTransaction(jobDataMap);
106             int ackMode = jobDataMap.getInt(JmsHelper.JMS_ACK_MODE);
107             session = conn.createQueueSession(useTransactions, ackMode);
108             queue = (Queue JavaDoc)ctx.lookup(JmsHelper.JMS_DESTINATION_JNDI);
109             sender = session.createSender(queue);
110             String JavaDoc factoryClass = jobDataMap.getString(JmsHelper.JMS_MSG_FACTORY_CLASS_NAME);
111             JmsMessageFactory factory = JmsHelper.getMessageFactory(factoryClass);
112             Message JavaDoc m = factory.createMessage(jobDataMap, session);
113             sender.send(m);
114         } catch (NamingException JavaDoc e) {
115             throw new JobExecutionException(e.getMessage());
116         } catch (JMSException JavaDoc e) {
117             throw new JobExecutionException(e.getMessage());
118         } catch (JmsJobException e) {
119             throw new JobExecutionException(e.getMessage());
120         } finally {
121             JmsHelper.closeResource(sender);
122             JmsHelper.closeResource(session);
123             JmsHelper.closeResource(conn);
124         }
125     }
126 }
127
Popular Tags