KickJava   Java API By Example, From Geeks To Geeks.

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


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.messaging.MessageExchange;
20 import javax.jbi.messaging.NormalizedMessage;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.servicemix.components.util.OutBinding;
29 import org.springframework.jms.core.JmsTemplate;
30 import org.springframework.jms.core.MessageCreator;
31
32 /**
33  * Consumers JBI messages and sends them to a JMS destination using the Spring
34  * {@link JmsTemplate}
35  *
36  * @version $Revision: 426415 $
37  */

38 public class JmsSenderComponent extends OutBinding {
39
40     private static final Log log = LogFactory.getLog(JmsSenderComponent.class);
41
42     private JmsTemplate template;
43     private JmsMarshaler marshaler = new JmsMarshaler();
44
45
46     // Properties
47
//-------------------------------------------------------------------------
48
public JmsTemplate getTemplate() {
49         return template;
50     }
51
52     public void setTemplate(JmsTemplate template) {
53         this.template = template;
54     }
55
56     public JmsMarshaler getMarshaler() {
57         return marshaler;
58     }
59
60     public void setMarshaler(JmsMarshaler marshaler) {
61         this.marshaler = marshaler;
62     }
63
64     /**
65      * @return Returns the needJavaIdentifiers.
66      */

67     public boolean isNeedJavaIdentifiers() {
68         return this.marshaler.isNeedJavaIdentifiers();
69     }
70
71     /**
72      * @param needJavaIdentifiers The needJavaIdentifiers to set.
73      */

74     public void setNeedJavaIdentifiers(boolean needJavaIdentifiers) {
75         this.marshaler.setNeedJavaIdentifiers(needJavaIdentifiers);
76     }
77     
78     // Implementation methods
79
//-------------------------------------------------------------------------
80
protected void process(MessageExchange exchange, final NormalizedMessage inMessage) throws Exception JavaDoc {
81         template.send(new MessageCreator() {
82             public Message createMessage(Session JavaDoc session) throws JMSException JavaDoc {
83                 try {
84                     Message message = marshaler.createMessage(inMessage, session);
85                     if (log.isTraceEnabled()) {
86                         log.trace("Sending message to: " + template.getDefaultDestinationName() + " message: " + message);
87                     }
88                     return message;
89                 }
90                 catch (TransformerException JavaDoc e) {
91                     JMSException JavaDoc jmsEx = new JMSException JavaDoc("Failed to create JMS Message: " + e);
92                     jmsEx.setLinkedException(e);
93                     throw jmsEx;
94                 }
95             }
96         });
97         done(exchange);
98     }
99 }
100
Popular Tags