KickJava   Java API By Example, From Geeks To Geeks.

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


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.servicemix.components.util.ComponentSupport;
20 import org.apache.servicemix.jbi.jaxp.StringSource;
21 import org.apache.servicemix.jbi.resolver.EndpointResolver;
22 import org.apache.servicemix.jbi.resolver.NullEndpointFilter;
23
24 import javax.jbi.JBIException;
25 import javax.jbi.component.ComponentContext;
26 import javax.jbi.messaging.InOnly;
27 import javax.jbi.messaging.MessagingException;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.jbi.servicedesc.ServiceEndpoint;
30 import javax.xml.namespace.QName JavaDoc;
31
32 /**
33  * @version $Revision: 426415 $
34  */

35 public class SenderComponent extends ComponentSupport implements Sender {
36     public static final QName JavaDoc SERVICE = new QName JavaDoc("http://servicemix.org/example/", "sender");
37     public static final String JavaDoc ENDPOINT = "sender";
38
39     private EndpointResolver resolver;
40     private String JavaDoc message = "<s12:Envelope xmlns:s12='http://www.w3.org/2003/05/soap-envelope'><s12:Body> <foo>Hello!</foo> </s12:Body></s12:Envelope>";
41
42     public SenderComponent() {
43         super(SERVICE, ENDPOINT);
44     }
45
46     public EndpointResolver getResolver() {
47         return resolver;
48     }
49
50     public void setResolver(EndpointResolver resolver) {
51         this.resolver = resolver;
52     }
53
54     public void sendMessages(int messageCount) throws JBIException {
55         sendMessages(messageCount, false);
56     }
57         
58     public void sendMessages(int messageCount, boolean sync) throws JBIException {
59         ComponentContext context = getContext();
60
61         for (int i = 0; i < messageCount; i++) {
62             InOnly exchange = context.getDeliveryChannel().createExchangeFactory().createInOnlyExchange();
63             NormalizedMessage message = exchange.createMessage();
64
65             ServiceEndpoint destination = null;
66             if (resolver != null) {
67                 destination = resolver.resolveEndpoint(getContext(), exchange, NullEndpointFilter.getInstance());
68             }
69             if (destination != null) {
70                 // lets explicitly specify the destination - otherwise
71
// we'll let the container choose for us
72
exchange.setEndpoint(destination);
73             }
74
75             exchange.setInMessage(message);
76             // lets set the XML as a byte[], String or DOM etc
77
message.setContent(new StringSource(this.message));
78             if (sync) {
79                 boolean result = context.getDeliveryChannel().sendSync(exchange, 1000);
80                 if (!result) {
81                     throw new MessagingException("Message delivery using sendSync has timed out");
82                 }
83             } else {
84                 context.getDeliveryChannel().send(exchange);
85             }
86         }
87     }
88
89     public String JavaDoc getMessage() {
90         return message;
91     }
92
93     public void setMessage(String JavaDoc message) {
94         this.message = message;
95     }
96
97 }
98
Popular Tags