KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jms > JmsInBinding


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.components.jms;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOnly;
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.MessagingException;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.MessageExchangeListener;
32 import org.apache.servicemix.components.util.ComponentSupport;
33 import org.apache.servicemix.jbi.RuntimeJBIException;
34
35 /**
36  * A JMS {@link MessageListener} which sends the inbound JMS message into the JBI container
37  * for processing
38  *
39  * @version $Revision: 426415 $
40  */

41 public class JmsInBinding extends ComponentSupport implements MessageListener JavaDoc, MessageExchangeListener {
42     private static final Log log = LogFactory.getLog(JmsInBinding.class);
43
44     private JmsMarshaler marshaler = new JmsMarshaler();
45
46     public JmsMarshaler getMarshaler() {
47         return marshaler;
48     }
49
50     public void setMarshaler(JmsMarshaler marshaler) {
51         this.marshaler = marshaler;
52     }
53
54     public void onMessage(Message jmsMessage) {
55         if (log.isTraceEnabled()) {
56             log.trace("Received: " + jmsMessage);
57         }
58
59         try {
60             InOnly messageExchange = getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
61             NormalizedMessage inMessage = messageExchange.createMessage();
62
63             try {
64                 marshaler.toNMS(inMessage, jmsMessage);
65
66                 messageExchange.setInMessage(inMessage);
67                 getDeliveryChannel().send(messageExchange);
68             }
69             catch (JMSException JavaDoc e) {
70                 messageExchange.setError(e);
71                 messageExchange.setStatus(ExchangeStatus.ERROR);
72             }
73         }
74         catch (JBIException e) {
75             throw new RuntimeJBIException(e);
76         }
77     }
78
79     public void onMessageExchange(MessageExchange exchange) throws MessagingException {
80         // Do nothing as we only send in-only
81
// but this ensure that messages are not queued in the DeliveryChannel
82
}
83 }
84
Popular Tags