KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > JMSMessage


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
20
21
22 /**
23  * Sends out a JMS TextMessage to a specified Queue or Topic. The following arguments
24  * are expected:
25  *
26  * <ul>
27  * <li>queue-factory-location - the location to be passed to InitialContext.lookup</li>
28  * <li>queue-location - the location to be passed to InitialContext.lookup</li>
29  * <li>topic-factory-location - the location to be passed to InitialContext.lookup</li>
30  * <li>topic-location - the location to be passed to InitialContext.lookup</li>
31  * <li>text - the text message to be included in this JMS message</li>
32  * </ul>
33  *
34  * Also, please note that the entire set of properties will be passed through to the
35  * constructor for InitialContext, meaning that if you need to use an
36  * InintialContextFactory other than the default one, you are free to include arguments
37  * that will do so.
38  *
39  * Also note that all arguments are also passed to the TextMessage using
40  * setObjectProperty(), except for "text" which is set using setText(). An extra
41  * property is always added to denote the workflow entry for this message. This is
42  * stored as a long property, with the name 'workflowEntry'.
43  *
44  * @author Hani Suleiman
45  * @version $Revision: 1.3 $
46  */

47 public class JMSMessage implements FunctionProvider {
48     //~ Static fields/initializers /////////////////////////////////////////////
49

50     private static final Log log = LogFactory.getLog(JMSMessage.class);
51
52     //~ Methods ////////////////////////////////////////////////////////////////
53

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 JavaDoc initialContext = new InitialContext JavaDoc(env);
60
61             if (args.containsKey("queue-factory-location")) {
62                 QueueConnectionFactory queueFactory = (QueueConnectionFactory) initialContext.lookup((String JavaDoc) 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 JavaDoc queue = (javax.jms.Queue JavaDoc) initialContext.lookup((String JavaDoc) 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 JavaDoc) 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 JavaDoc) 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 JavaDoc 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 JavaDoc) 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             // don't include "text", it was already done
98
if (!"text".equals(mapEntry.getKey())) {
99                 message.setObjectProperty((String JavaDoc) mapEntry.getKey(), mapEntry.getValue());
100             }
101         }
102     }
103 }
104
Popular Tags