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.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueSender ; 29 import javax.jms.QueueSession ; 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 77 public class SendQueueMessageJob implements Job { 78 79 public void execute(JobExecutionContext context) 80 throws JobExecutionException { 81 82 83 QueueConnectionFactory qcf = null; 84 QueueConnection conn = null; 85 QueueSession session = null; 86 Queue queue = null; 87 QueueSender sender = null; 88 InitialContext 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 user = jobDataMap.getString(JmsHelper.JMS_USER); 99 String 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 )ctx.lookup(JmsHelper.JMS_DESTINATION_JNDI); 109 sender = session.createSender(queue); 110 String factoryClass = jobDataMap.getString(JmsHelper.JMS_MSG_FACTORY_CLASS_NAME); 111 JmsMessageFactory factory = JmsHelper.getMessageFactory(factoryClass); 112 Message m = factory.createMessage(jobDataMap, session); 113 sender.send(m); 114 } catch (NamingException e) { 115 throw new JobExecutionException(e.getMessage()); 116 } catch (JMSException 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 |