1 5 package com.opensymphony.workflow.util; 6 7 import com.opensymphony.module.propertyset.PropertySet; 8 9 import com.opensymphony.workflow.FunctionProvider; 10 import com.opensymphony.workflow.spi.WorkflowEntry; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import java.util.*; 16 17 import javax.jms.*; 18 19 import javax.naming.InitialContext ; 20 21 22 47 public class JMSMessage implements FunctionProvider { 48 50 private static final Log log = LogFactory.getLog(JMSMessage.class); 51 52 54 public void execute(Map transientVars, Map args, PropertySet ps) { 55 WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry"); 56 57 try { 58 Hashtable env = new Hashtable(args); 59 InitialContext initialContext = new InitialContext (env); 60 61 if (args.containsKey("queue-factory-location")) { 62 QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext.lookup((String ) args.get("queue-factory-location")); 63 QueueConnection conn = queueFactory.createQueueConnection(); 64 conn.start(); 65 66 QueueSession queueSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 67 javax.jms.Queue queue = (javax.jms.Queue ) initialContext.lookup((String ) args.get("queue-location")); 68 QueueSender sender = queueSession.createSender(queue); 69 TextMessage message = queueSession.createTextMessage(); 70 populateMessage(message, entry, args); 71 sender.send(message); 72 } else if (args.containsKey("topic-factory-location")) { 73 TopicConnectionFactory topicFactory = (TopicConnectionFactory) initialContext.lookup((String ) args.get("topic-factory-location")); 74 TopicConnection conn = topicFactory.createTopicConnection(); 75 conn.start(); 76 77 TopicSession topicSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 78 Topic topic = (Topic) initialContext.lookup((String ) args.get("topic-location")); 79 TopicPublisher publisher = topicSession.createPublisher(topic); 80 TextMessage message = topicSession.createTextMessage(); 81 populateMessage(message, entry, args); 82 publisher.publish(message); 83 } 84 } catch (Exception ex) { 85 log.error("Error sending JMS message", ex); 86 } 87 } 88 89 private void populateMessage(TextMessage message, WorkflowEntry entry, Map properties) throws JMSException { 90 message.setText((String ) properties.get("text")); 91 message.setLongProperty("workflowEntry", entry.getId()); 92 93 for (Iterator iterator = properties.entrySet().iterator(); 94 iterator.hasNext();) { 95 Map.Entry mapEntry = (Map.Entry) iterator.next(); 96 97 if (!"text".equals(mapEntry.getKey())) { 99 message.setObjectProperty((String ) mapEntry.getKey(), mapEntry.getValue()); 100 } 101 } 102 } 103 } 104 | Popular Tags |