KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > tck > SenderPojo


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.tck;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.components.util.PojoSupport;
22 import org.apache.servicemix.jbi.jaxp.StringSource;
23
24 import javax.jbi.JBIException;
25 import javax.jbi.component.ComponentContext;
26 import javax.jbi.component.ComponentLifeCycle;
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.InOnly;
29 import javax.jbi.messaging.MessagingException;
30 import javax.jbi.messaging.NormalizedMessage;
31 import javax.jbi.servicedesc.ServiceEndpoint;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * @version $Revision: 426415 $
39  */

40 public class SenderPojo extends PojoSupport implements ComponentLifeCycle, Sender {
41
42     public static final QName JavaDoc SERVICE = new QName JavaDoc("http://servicemix.org/example/", "sender");
43     public static final String JavaDoc ENDPOINT = "sender";
44
45     private static final Log log = LogFactory.getLog(SenderPojo.class);
46
47     private QName JavaDoc interfaceName;
48     private int numMessages = 10;
49     private ComponentContext context;
50     private List JavaDoc messages = new ArrayList JavaDoc();
51     boolean done = false;
52
53     public SenderPojo() {
54         this(ReceiverPojo.SERVICE);
55     }
56
57     public SenderPojo(QName JavaDoc interfaceName) {
58         this.interfaceName = interfaceName;
59     }
60
61     public void init(ComponentContext context) throws JBIException {
62         this.context = context;
63         context.activateEndpoint(SERVICE, ENDPOINT);
64     }
65
66     public int messagesReceived() {
67         return messages.size();
68     }
69
70     public void sendMesssages() throws MessagingException {
71         sendMessages(numMessages);
72     }
73
74     public void sendMessages(int messageCount) throws MessagingException {
75         sendMessages(messageCount, true);
76     }
77     
78     public void sendMessages(int messageCount, boolean sync) throws MessagingException {
79         System.out.println("Looking for services for interface: " + interfaceName);
80
81         ServiceEndpoint[] endpoints = context.getEndpointsForService(interfaceName);
82         if (endpoints.length > 0) {
83             ServiceEndpoint endpoint = endpoints[0];
84             System.out.println("Sending to endpoint: " + endpoint);
85             
86             for (int i = 0; i < messageCount; i++) {
87                 InOnly exchange = context.getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
88                 NormalizedMessage message = exchange.createMessage();
89                 exchange.setEndpoint(endpoint);
90                 exchange.setInMessage(message);
91                 // lets set the XML as a byte[], String or DOM etc
92
String JavaDoc xml = "<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'><s12:Body> <foo>Hello!</foo> </s12:Body></s12:Envelope>";
93                 message.setContent(new StringSource(xml));
94                 System.out.println("sending message: " + i);
95                 DeliveryChannel deliveryChannel = context.getDeliveryChannel();
96                 System.out.println("sync send on deliverychannel: " + deliveryChannel);
97                 if (sync) {
98                     deliveryChannel.sendSync(exchange);
99                 } else {
100                     deliveryChannel.send(exchange);
101                 }
102             }
103         }
104         else {
105             log.warn("No endpoints available for interface: " + interfaceName);
106         }
107     }
108
109
110     // Properties
111
//-------------------------------------------------------------------------
112
public QName JavaDoc getInterfaceName() {
113         return interfaceName;
114     }
115
116     public void setInterfaceName(QName JavaDoc interfaceName) {
117         this.interfaceName = interfaceName;
118     }
119
120     public int getNumMessages() {
121         return numMessages;
122     }
123
124     public void setNumMessages(int numMessages) {
125         this.numMessages = numMessages;
126     }
127 }
128
Popular Tags