KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > wsif > SampleServiceMessageListener


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.wsif;
18
19
20 import org.springframework.beans.factory.InitializingBean;
21 import org.springframework.jms.core.JmsTemplate;
22 import org.springframework.jms.core.MessageCreator;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageListener JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29
30 /**
31  * This is a simple MDB that processes a message containing an integer
32  * that represents a zip code, and returns a message containing a
33  * boolean indicating whether DSL service is available at that zip code
34  * or not.
35  * Internally the bean just returns true for zip codes < 50000 and
36  * false otherwise.
37  *
38  * @version $Revision: 426415 $
39  */

40 public class SampleServiceMessageListener implements MessageListener JavaDoc, InitializingBean {
41     private JmsTemplate template;
42
43     public JmsTemplate getTemplate() {
44         return template;
45     }
46
47     public void setTemplate(JmsTemplate template) {
48         this.template = template;
49     }
50
51     public void afterPropertiesSet() throws Exception JavaDoc {
52         if (template == null) {
53             throw new IllegalArgumentException JavaDoc("The template property is not set");
54         }
55     }
56
57     public void onMessage(Message JavaDoc msg) {
58         try {
59             TextMessage JavaDoc message = (TextMessage JavaDoc) msg;
60             // assume we have an integer
61
String JavaDoc text = message.getText();
62             System.out.println("Text: " + text);
63             int zipCode = new Integer JavaDoc(text).intValue();
64             // return true if zip code < 50000, false otherwise
65
if (zipCode < 50000) {
66                 sendMessage(message, "true");
67             }
68             else {
69                 sendMessage(message, "false");
70             }
71         }
72         catch (Exception JavaDoc e) {
73             // aargh - this should not happen usually
74
System.out.println(e);
75             e.printStackTrace();
76         }
77     }
78
79     public void sendMessage(final TextMessage JavaDoc requestMsg, final String JavaDoc serviceAvailable) throws JMSException JavaDoc {
80         template.send(requestMsg.getJMSReplyTo(), new MessageCreator() {
81             public Message JavaDoc createMessage(Session JavaDoc session) throws JMSException JavaDoc {
82                 TextMessage JavaDoc message = session.createTextMessage();
83                 // set the correlation ID
84
message.setJMSCorrelationID(requestMsg.getJMSMessageID());
85                 // set the text
86
message.setText(serviceAvailable);
87                 return message;
88             }
89         });
90     }
91 }
92
93
Popular Tags