KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jms > standard > StandardConsumerProcessor


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

17 package org.apache.servicemix.jms.standard;
18
19 import javax.jbi.messaging.DeliveryChannel;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jms.Destination JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.resource.spi.work.Work JavaDoc;
29
30 import org.apache.servicemix.jms.AbstractJmsProcessor;
31 import org.apache.servicemix.jms.JmsEndpoint;
32 import org.apache.servicemix.soap.Context;
33
34 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
35
36 public class StandardConsumerProcessor extends AbstractJmsProcessor {
37
38     protected Session JavaDoc session;
39     protected Destination JavaDoc destination;
40     protected DeliveryChannel channel;
41     protected AtomicBoolean running = new AtomicBoolean(false);
42
43     public StandardConsumerProcessor(JmsEndpoint endpoint) {
44         super(endpoint);
45     }
46
47     protected void doStart(InitialContext JavaDoc ctx) throws Exception JavaDoc {
48         destination = endpoint.getDestination();
49         if (destination == null) {
50             if (endpoint.getJndiDestinationName() != null) {
51                 destination = (Destination JavaDoc) ctx.lookup(endpoint.getJndiDestinationName());
52             } else if (endpoint.getJmsProviderDestinationName() == null) {
53                 throw new IllegalStateException JavaDoc("No destination provided");
54             }
55         }
56         channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
57         synchronized (running) {
58             endpoint.getServiceUnit().getComponent().getWorkManager().startWork(new Work JavaDoc() {
59                 public void release() {
60                 }
61                 public void run() {
62                     StandardConsumerProcessor.this.poll();
63                 }
64             });
65             running.wait();
66         }
67     }
68
69     protected void doStop() throws Exception JavaDoc {
70         if (running.get()) {
71             synchronized (running) {
72                 if (session != null) {
73                     session.close();
74                 }
75                 running.wait();
76             }
77         }
78         session = null;
79         destination = null;
80     }
81
82     protected void poll() {
83         synchronized (running) {
84             running.set(true);
85             running.notify();
86         }
87         try {
88             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
89             if (destination == null) {
90                 if (STYLE_QUEUE.equals(endpoint.getDestinationStyle())) {
91                     destination = session.createQueue(endpoint.getJmsProviderDestinationName());
92                 } else {
93                     destination = session.createTopic(endpoint.getJmsProviderDestinationName());
94                 }
95             }
96             MessageConsumer JavaDoc consumer = session.createConsumer(destination);
97             while (running.get()) {
98                 Message JavaDoc message = consumer.receive();
99                 if (message != null) {
100                     onMessage(message);
101                 }
102             }
103         } catch (Exception JavaDoc e) {
104             log.error("", e);
105         } finally {
106             synchronized (running) {
107                 running.set(false);
108                 running.notify();
109             }
110         }
111     }
112
113     public void onMessage(final Message JavaDoc message) {
114         try {
115             if (log.isDebugEnabled()) {
116                 log.debug("Received jms message " + message);
117             }
118             Context JavaDoc context = createContext();
119             MessageExchange exchange = toNMS(message, context);
120             if (!channel.sendSync(exchange)) {
121                 throw new IllegalStateException JavaDoc("Exchange has been aborted");
122             }
123             MessageProducer JavaDoc producer = null;
124             Message JavaDoc response = null;
125             try {
126                 response = fromNMSResponse(exchange, context, session);
127                 if (response != null) {
128                     producer = session.createProducer(message.getJMSReplyTo());
129                     response.setJMSCorrelationID(message.getJMSCorrelationID());
130                     producer.send(response);
131                 }
132             } finally {
133                 if (producer != null) {
134                     producer.close();
135                 }
136                 if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
137                     exchange.setStatus(ExchangeStatus.DONE);
138                     channel.send(exchange);
139                 }
140             }
141         } catch (Throwable JavaDoc e) {
142             log.error("Error while handling jms message", e);
143         }
144     }
145
146     public void process(MessageExchange exchange) throws Exception JavaDoc {
147         throw new IllegalStateException JavaDoc();
148     }
149
150 }
151
Popular Tags