KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Session 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.Destination</code>. Note, this class can only be used in a JMS 1.1
43 * compliant environment.
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 SendDesitnationMessageJob implements Job {
79
80     public void execute(JobExecutionContext context)
81         throws JobExecutionException {
82         
83         ConnectionFactory JavaDoc factory = null;
84         Connection JavaDoc connection = null;
85         Session JavaDoc session = null;
86         MessageProducer JavaDoc producer = null;
87         Destination JavaDoc destination = null;
88         InitialContext JavaDoc initCtx = null;
89         
90         JobDetail detail = context.getJobDetail();
91         JobDataMap jobDataMap = detail.getJobDataMap();
92         
93         try {
94             
95             initCtx = org.quartz.jobs.ee.jms.JmsHelper.getInitialContext(jobDataMap);
96             factory = (ConnectionFactory JavaDoc) initCtx.lookup(org.quartz.jobs.ee.jms.JmsHelper.JMS_CONNECTION_FACTORY_JNDI);
97             
98             if(org.quartz.jobs.ee.jms.JmsHelper.isDestinationSecure(jobDataMap))
99             {
100                 String JavaDoc user = jobDataMap.getString(org.quartz.jobs.ee.jms.JmsHelper.JMS_USER);
101                 String JavaDoc pw = jobDataMap.getString(org.quartz.jobs.ee.jms.JmsHelper.JMS_PASSWORD);
102                 connection = factory.createConnection(user, pw);
103             } else {
104                 connection = factory.createConnection();
105                     
106             }
107                         
108             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
109             destination = (Destination JavaDoc) initCtx.lookup(org.quartz.jobs.ee.jms.JmsHelper.JMS_DESTINATION_JNDI);
110             producer = session.createProducer(destination);
111             String JavaDoc factoryClass = jobDataMap.getString(org.quartz.jobs.ee.jms.JmsHelper.JMS_MSG_FACTORY_CLASS_NAME);
112             org.quartz.jobs.ee.jms.JmsMessageFactory messageFactory = org.quartz.jobs.ee.jms.JmsHelper.getMessageFactory(factoryClass);
113             Message JavaDoc m = messageFactory.createMessage(jobDataMap, session);
114             producer.send(m);
115
116         } catch (NamingException JavaDoc e) {
117             
118             throw new JobExecutionException(e);
119             
120         } catch (JMSException JavaDoc e) {
121
122             throw new JobExecutionException(e);
123
124         } catch (JmsJobException e) {
125
126             throw new JobExecutionException(e);
127         
128         }finally{
129             
130             JmsHelper.closeResource(producer);
131             JmsHelper.closeResource(session);
132             JmsHelper.closeResource(connection);
133         }
134
135         
136     }
137
138 }
139
Popular Tags