1 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 ; 33 34 import java.util.ArrayList ; 35 import java.util.List ; 36 37 40 public class SenderPojo extends PojoSupport implements ComponentLifeCycle, Sender { 41 42 public static final QName SERVICE = new QName ("http://servicemix.org/example/", "sender"); 43 public static final String ENDPOINT = "sender"; 44 45 private static final Log log = LogFactory.getLog(SenderPojo.class); 46 47 private QName interfaceName; 48 private int numMessages = 10; 49 private ComponentContext context; 50 private List messages = new ArrayList (); 51 boolean done = false; 52 53 public SenderPojo() { 54 this(ReceiverPojo.SERVICE); 55 } 56 57 public SenderPojo(QName 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 String 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 public QName getInterfaceName() { 113 return interfaceName; 114 } 115 116 public void setInterfaceName(QName 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 |