1 17 18 21 package org.quartz.jobs.ee.jms; 22 23 import javax.jms.JMSException ; 24 import javax.jms.Message ; 25 import javax.jms.Session ; 26 import javax.jms.Topic ; 27 import javax.jms.TopicConnection ; 28 import javax.jms.TopicConnectionFactory ; 29 import javax.jms.TopicPublisher ; 30 import javax.jms.TopicSession ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 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 78 public class SendTopicMessageJob implements Job { 79 80 public void execute(JobExecutionContext context) 81 throws JobExecutionException { 82 83 TopicConnectionFactory tcf = null; 84 TopicConnection connection = null; 85 TopicSession session = null; 86 Topic topic = null; 87 TopicPublisher publisher = null; 88 InitialContext ctx = null; 89 String user = null; 90 String 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 ) 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 ) ctx.lookup(JmsHelper.JMS_DESTINATION_JNDI); 116 publisher = session.createPublisher(topic); 117 String factoryClassName = jobDataMap.getString(JmsHelper.JMS_MSG_FACTORY_CLASS_NAME); 118 JmsMessageFactory factory = JmsHelper.getMessageFactory(factoryClassName); 119 Message m = factory.createMessage(jobDataMap, session); 120 publisher.publish(m); 121 122 } catch (NamingException e) { 123 124 throw new JobExecutionException(e); 125 126 } catch (JMSException 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 |