1 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 ; 31 32 35 public class SenderComponent extends ComponentSupport implements Sender { 36 public static final QName SERVICE = new QName ("http://servicemix.org/example/", "sender"); 37 public static final String ENDPOINT = "sender"; 38 39 private EndpointResolver resolver; 40 private String 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 exchange.setEndpoint(destination); 73 } 74 75 exchange.setInMessage(message); 76 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 getMessage() { 90 return message; 91 } 92 93 public void setMessage(String message) { 94 this.message = message; 95 } 96 97 } 98 | Popular Tags |