1 17 package org.apache.servicemix.http; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.net.URI ; 22 import java.net.URL ; 23 24 import javax.jbi.messaging.ExchangeStatus; 25 import javax.jbi.messaging.InOut; 26 import javax.jbi.messaging.RobustInOnly; 27 import javax.wsdl.Definition; 28 import javax.wsdl.factory.WSDLFactory; 29 import javax.xml.namespace.QName ; 30 import javax.xml.transform.stream.StreamSource ; 31 32 import junit.framework.TestCase; 33 34 import org.apache.servicemix.client.DefaultServiceMixClient; 35 import org.apache.servicemix.components.http.HttpInvoker; 36 import org.apache.servicemix.components.http.HttpSoapClientMarshaler; 37 import org.apache.servicemix.components.util.EchoComponent; 38 import org.apache.servicemix.jbi.container.ActivationSpec; 39 import org.apache.servicemix.jbi.container.JBIContainer; 40 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 41 import org.apache.servicemix.tck.Receiver; 42 import org.apache.servicemix.tck.ReceiverComponent; 43 44 public class HttpConsumerTest extends TestCase { 45 46 protected JBIContainer container; 47 48 protected void setUp() throws Exception { 49 container = new JBIContainer(); 50 container.setUseMBeanServer(false); 51 container.setCreateMBeanServer(false); 52 container.setEmbedded(true); 53 container.init(); 54 } 55 56 protected void tearDown() throws Exception { 57 if (container != null) { 58 container.shutDown(); 59 } 60 } 61 62 protected long testInOnly(String msg, boolean streaming) throws Exception { 63 HttpComponent component = new HttpComponent(); 65 ((HttpLifeCycle) component.getLifeCycle()).getConfiguration().setStreamingEnabled(streaming); 66 container.activateComponent(component, "HTTPComponent"); 67 68 Receiver receiver = new ReceiverComponent(); 70 ActivationSpec asReceiver = new ActivationSpec("receiver", receiver); 71 asReceiver.setService(new QName ("http://http.servicemix.org/Test", "ConsumerInOnly")); 72 container.activateComponent(asReceiver); 73 74 HttpInvoker invoker = new HttpInvoker(); 76 invoker.setDefaultInOut(false); 77 invoker.setUrl("http://localhost:8192/InOnly/"); 78 invoker.setMarshaler(new HttpSoapClientMarshaler(true)); 79 ActivationSpec asInvoker = new ActivationSpec("invoker", invoker); 80 asInvoker.setService(new QName ("urn:test", "invoker")); 81 container.activateComponent(asInvoker); 82 83 container.start(); 85 86 URL url = getClass().getClassLoader().getResource("consumer/http.wsdl"); 88 File path = new File (new URI (url.toString())); 89 path = path.getParentFile(); 90 component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath()); 91 component.getServiceUnitManager().start("consumer"); 92 93 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 95 RobustInOnly in = client.createRobustInOnlyExchange(); 96 in.setService(new QName ("urn:test", "invoker")); 97 in.getInMessage().setContent(new StreamSource (new ByteArrayInputStream (msg.getBytes()))); 98 99 long t0 = System.currentTimeMillis(); 100 client.sendSync(in); 101 long t1 = System.currentTimeMillis(); 102 assertEquals(ExchangeStatus.DONE, in.getStatus()); 103 104 receiver.getMessageList().assertMessagesReceived(1); 106 107 return t1 - t0; 108 } 109 110 111 protected long testInOut(String msg, boolean streaming) throws Exception { 112 HttpComponent component = new HttpComponent(); 114 ((HttpLifeCycle) component.getLifeCycle()).getConfiguration().setStreamingEnabled(streaming); 115 container.activateComponent(component, "HTTPComponent"); 116 117 EchoComponent echo = new EchoComponent(); 119 ActivationSpec asReceiver = new ActivationSpec("echo", echo); 120 asReceiver.setService(new QName ("http://http.servicemix.org/Test", "ConsumerInOut")); 121 container.activateComponent(asReceiver); 122 123 HttpInvoker invoker = new HttpInvoker(); 125 invoker.setDefaultInOut(true); 126 invoker.setUrl("http://localhost:8192/InOut/"); 127 ActivationSpec asInvoker = new ActivationSpec("invoker", invoker); 128 asInvoker.setService(new QName ("urn:test", "invoker")); 129 container.activateComponent(asInvoker); 130 131 container.start(); 133 134 URL url = getClass().getClassLoader().getResource("consumer/http.wsdl"); 136 File path = new File (new URI (url.toString())); 137 path = path.getParentFile(); 138 component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath()); 139 component.getServiceUnitManager().start("consumer"); 140 141 Definition def = WSDLFactory.newInstance().newWSDLReader().readWSDL("http://localhost:8192/InOut/?wsdl"); 143 assertNotNull(def); 144 145 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 147 InOut inout = client.createInOutExchange(); 148 inout.setService(new QName ("urn:test", "invoker")); 149 inout.getInMessage().setContent(new StreamSource (new ByteArrayInputStream (msg.getBytes()))); 150 151 long t0 = System.currentTimeMillis(); 152 client.sendSync(inout); 153 long t1 = System.currentTimeMillis(); 154 assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE); 155 156 assertNotNull(inout.getOutMessage()); 158 assertNotNull(inout.getOutMessage().getContent()); 159 System.out.println(new SourceTransformer().toString(inout.getOutMessage().getContent())); 160 161 return t1 - t0; 162 } 163 164 public void testInOnly() throws Exception { 165 testInOnly("<hello>world</hello>", false); 166 } 167 168 public void testInOut() throws Exception { 169 testInOut("<hello>world</hello>", true); 170 } 171 172 public void testPerfInOnlyWithBigMessage() throws Exception { 173 int nbRuns = 2; 174 int sizeInKb = 64; 175 176 StringBuffer sb = new StringBuffer (); 177 sb.append("<hello>"); 178 for (int i = 0; i < sizeInKb; i++) { 179 sb.append("<hello>"); 180 for (int j = 0; j < 1024 - 15; j++) { 181 sb.append((char) ('A' + (int)(Math.random() * ('Z' - 'A' + 1)))); 182 } 183 sb.append("</hello>"); 184 } 185 sb.append("</hello>"); 186 String str = sb.toString(); 187 188 for(int i = 0; i < nbRuns; i++) { 189 System.gc(); 190 long dt = testInOnly(str, false); 191 System.err.println("No Streaming: " + dt); 192 tearDown(); 193 setUp(); 194 } 195 196 for(int i = 0; i < nbRuns; i++) { 197 System.gc(); 198 long dt = testInOnly(str, true); 199 System.err.println("Streaming: " + dt); 200 tearDown(); 201 setUp(); 202 } 203 } 204 205 } 206 | Popular Tags |