KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > jms > SimpleJMSWorker


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.jms;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.server.AxisServer;
24 import org.apache.axis.utils.Messages;
25 import org.apache.commons.logging.Log;
26
27 import javax.jms.BytesMessage JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32
33 /**
34  * SimpleJMSWorker is a worker thread that processes messages that are
35  * received by SimpleJMSListener. It creates a new message context, invokes
36  * the server, and sends back response msg to the replyTo destination.
37  *
38  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
39  * @author Richard Chung (rchung@sonicsoftware.com)
40  * @author Dave Chappell (chappell@sonicsoftware.com)
41  */

42 public class SimpleJMSWorker implements Runnable JavaDoc
43 {
44     protected static Log log =
45             LogFactory.getLog(SimpleJMSWorker.class.getName());
46
47     SimpleJMSListener listener;
48     BytesMessage JavaDoc message;
49
50     public SimpleJMSWorker(SimpleJMSListener listener, BytesMessage JavaDoc message)
51     {
52         this.listener = listener;
53         this.message = message;
54     }
55
56     /**
57      * This is where the incoming message is processed.
58      */

59     public void run()
60     {
61         InputStream JavaDoc in = null;
62         try
63         {
64             // get the incoming msg content into a byte array
65
byte[] buffer = new byte[8 * 1024];
66             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
67             for(int bytesRead = message.readBytes(buffer);
68                 bytesRead != -1; bytesRead = message.readBytes(buffer))
69             {
70                 out.write(buffer, 0, bytesRead);
71             }
72             in = new ByteArrayInputStream JavaDoc(out.toByteArray());
73         }
74         catch(Exception JavaDoc e)
75         {
76             log.error(Messages.getMessage("exception00"), e);
77             e.printStackTrace();
78             return;
79         }
80
81         // create the msg and context and invoke the server
82
AxisServer server = SimpleJMSListener.getAxisServer();
83
84         // if the incoming message has a contentType set,
85
// pass it to my new Message
86
String JavaDoc contentType = null;
87         try
88         {
89             contentType = message.getStringProperty("contentType");
90         }
91         catch(Exception JavaDoc e)
92         {
93             e.printStackTrace();
94         }
95
96         Message msg = null;
97         if(contentType != null && !contentType.trim().equals(""))
98         {
99             msg = new Message(in, true, contentType, null);
100         }
101         else
102         {
103             msg = new Message(in);
104         }
105
106         MessageContext msgContext = new MessageContext(server);
107         msgContext.setRequestMessage( msg );
108         try
109         {
110             server.invoke( msgContext );
111             msg = msgContext.getResponseMessage();
112         }
113         catch (AxisFault af)
114         {
115             msg = new Message(af);
116             msg.setMessageContext(msgContext);
117         }
118         catch (Exception JavaDoc e)
119         {
120             msg = new Message(new AxisFault(e.toString()));
121             msg.setMessageContext(msgContext);
122         }
123
124         try
125         {
126             // now we need to send the response
127
Destination JavaDoc destination = message.getJMSReplyTo();
128             if(destination == null)
129                 return;
130             JMSEndpoint replyTo = listener.getConnector().createEndpoint(destination);
131             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
132             msg.writeTo(out);
133             replyTo.send(out.toByteArray());
134         }
135         catch(Exception JavaDoc e)
136         {
137             e.printStackTrace();
138         }
139
140         if (msgContext.getProperty(MessageContext.QUIT_REQUESTED) != null)
141             // why then, quit!
142
try {listener.shutdown();} catch (Exception JavaDoc e) {}
143     }
144 }
145
Popular Tags