1 17 18 21 package org.quartz.jobs.ee.jms; 22 23 import javax.jms.Connection ; 24 import javax.jms.ConnectionFactory ; 25 import javax.jms.Destination ; 26 import javax.jms.JMSException ; 27 import javax.jms.Message ; 28 import javax.jms.MessageProducer ; 29 import javax.jms.Session ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 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 78 public class SendDesitnationMessageJob implements Job { 79 80 public void execute(JobExecutionContext context) 81 throws JobExecutionException { 82 83 ConnectionFactory factory = null; 84 Connection connection = null; 85 Session session = null; 86 MessageProducer producer = null; 87 Destination destination = null; 88 InitialContext 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 ) 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 user = jobDataMap.getString(org.quartz.jobs.ee.jms.JmsHelper.JMS_USER); 101 String 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 ) initCtx.lookup(org.quartz.jobs.ee.jms.JmsHelper.JMS_DESTINATION_JNDI); 110 producer = session.createProducer(destination); 111 String 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 m = messageFactory.createMessage(jobDataMap, session); 114 producer.send(m); 115 116 } catch (NamingException e) { 117 118 throw new JobExecutionException(e); 119 120 } catch (JMSException 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 |