001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.jbi.component;
018:
019: import java.util.Date;
020: import java.util.Properties;
021: import java.util.concurrent.ExecutorService;
022: import java.util.concurrent.Executors;
023: import java.util.concurrent.TimeUnit;
024:
025: import javax.jbi.messaging.ExchangeStatus;
026: import javax.jbi.messaging.InOut;
027: import javax.xml.namespace.QName;
028:
029: import org.apache.activemq.xbean.BrokerFactoryBean;
030: import org.apache.log4j.PropertyConfigurator;
031: import org.apache.servicemix.client.DefaultServiceMixClient;
032: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
033: import org.apache.servicemix.jbi.jaxp.StringSource;
034: import org.apache.servicemix.tck.SpringTestSupport;
035: import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
036: import org.springframework.context.support.AbstractXmlApplicationContext;
037: import org.springframework.core.io.ClassPathResource;
038:
039: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
040:
041: public class FastLoanBrokerTest extends SpringTestSupport {
042:
043: private final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
044: .getLog(FastLoanBrokerTest.class);
045:
046: private BrokerFactoryBean bean;
047:
048: @Override
049: protected void setUp() throws Exception {
050: bean = new BrokerFactoryBean();
051: bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
052: bean.afterPropertiesSet();
053: super .setUp();
054: }
055:
056: @Override
057: protected void tearDown() throws Exception {
058: super .tearDown();
059: bean.destroy();
060: }
061:
062: public FastLoanBrokerTest() {
063: Properties properties = new Properties();
064: properties.put("log4j.rootCategory", "INFO, stdout");
065: properties.put("log4j.appender.stdout",
066: "org.apache.log4j.ConsoleAppender");
067: properties.put("log4j.appender.stdout.layout",
068: "org.apache.log4j.PatternLayout");
069: properties.put(
070: "log4j.appender.stdout.layout.ConversionPattern",
071: "%p [%t] %c{1}.%M(%L) | %m%n");
072: PropertyConfigurator.configure(properties);
073: }
074:
075: public void testFastLoanBroker() throws Exception {
076: final DefaultServiceMixClient client = new DefaultServiceMixClient(
077: jbi);
078: {
079: InOut me = client.createInOutExchange();
080: me.setService(new QName("http://bpmscript.org/jbi",
081: "loanbroker"));
082: me.getInMessage().setContent(
083: new StringSource("<hello>world</hello>"));
084: assertTrue(client.sendSync(me, 1000000));
085: if (me.getStatus() == ExchangeStatus.ERROR) {
086: if (me.getFault() != null) {
087: fail("Received fault: "
088: + new SourceTransformer().toString(me
089: .getFault().getContent()));
090: } else if (me.getError() != null) {
091: throw me.getError();
092: } else {
093: fail("Received ERROR status");
094: }
095: }
096: }
097: int total = 1000;
098: long startTime = System.currentTimeMillis();
099: ExecutorService executorService = Executors
100: .newFixedThreadPool(10);
101: final AtomicInteger count = new AtomicInteger(0);
102: for (int i = 0; i < total; i++) {
103: executorService.execute(new Runnable() {
104: public void run() {
105: try {
106: InOut me = client.createInOutExchange();
107: me.setService(new QName(
108: "http://bpmscript.org/jbi",
109: "loanbroker"));
110: me
111: .getInMessage()
112: .setContent(
113: new StringSource(
114: "<hello>world</hello>"));
115: assertTrue(client.sendSync(me, 1000000));
116: if (me.getStatus() == ExchangeStatus.ERROR) {
117: if (me.getFault() != null) {
118: fail("Received fault: "
119: + new SourceTransformer()
120: .toString(me.getFault()
121: .getContent()));
122: } else if (me.getError() != null) {
123: throw me.getError();
124: } else {
125: fail("Received ERROR status");
126: }
127: }
128: count.incrementAndGet();
129: } catch (Throwable t) {
130: fail(t.getMessage());
131: }
132: }
133:
134: });
135: }
136: executorService.shutdown();
137: while (!executorService.awaitTermination(2, TimeUnit.SECONDS)) {
138: log
139: .info(new Date() + ": " + count.get()
140: + " have finished");
141: }
142: long duration = System.currentTimeMillis() - startTime;
143: log.info("Average time " + ((double) duration / (double) total)
144: + "ms");
145: log.info("Average throughput "
146: + (((double) total / (double) duration) * 60000.0)
147: + "tpm");
148: }
149:
150: protected AbstractXmlApplicationContext createBeanFactory() {
151: return new ClassPathXmlApplicationContext(
152: "/fastloanbroker/spring.xml");
153: }
154:
155: }
|