001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.mdb;
018:
019: import java.util.concurrent.Executor;
020: import java.util.concurrent.Executors;
021: import javax.jms.Connection;
022: import javax.jms.ConnectionFactory;
023: import javax.jms.Destination;
024: import javax.jms.MessageConsumer;
025: import javax.jms.Session;
026: import javax.resource.spi.BootstrapContext;
027: import javax.resource.spi.ResourceAdapterInternalException;
028: import javax.resource.spi.work.WorkManager;
029:
030: import junit.framework.TestCase;
031: import org.apache.activemq.ActiveMQConnectionFactory;
032: import org.apache.geronimo.connector.GeronimoBootstrapContext;
033: import org.apache.geronimo.connector.work.GeronimoWorkManager;
034: import org.apache.geronimo.transaction.manager.GeronimoTransactionManager;
035: import org.apache.openejb.OpenEJBException;
036: import org.apache.openejb.resource.activemq.ActiveMQResourceAdapter;
037:
038: public class JmsProxyTest extends TestCase {
039: private static final String REQUEST_QUEUE_NAME = "request";
040: private ConnectionFactory connectionFactory;
041: private ActiveMQResourceAdapter ra;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045:
046: // create a transaction manager
047: GeronimoTransactionManager transactionManager = new GeronimoTransactionManager();
048:
049: // create the ActiveMQ resource adapter instance
050: ra = new ActiveMQResourceAdapter();
051:
052: // initialize properties
053: ra.setServerUrl("tcp://localhost:61616");
054: ra
055: .setBrokerXmlConfig("broker:(tcp://localhost:61616)?useJmx=false");
056:
057: // create a thead pool for ActiveMQ
058: Executor threadPool = Executors.newFixedThreadPool(30);
059:
060: // create a work manager which ActiveMQ uses to dispatch message delivery jobs
061: WorkManager workManager = new GeronimoWorkManager(threadPool,
062: threadPool, threadPool, transactionManager);
063:
064: // wrap the work mananger and transaction manager in a bootstrap context (connector spec thing)
065: BootstrapContext bootstrapContext = new GeronimoBootstrapContext(
066: workManager, transactionManager);
067:
068: // start the resource adapter
069: try {
070: ra.start(bootstrapContext);
071: } catch (ResourceAdapterInternalException e) {
072: throw new OpenEJBException(e);
073: }
074: // Create a ConnectionFactory
075: connectionFactory = new ActiveMQConnectionFactory(
076: "tcp://localhost:61616");
077: }
078:
079: protected void tearDown() throws Exception {
080: connectionFactory = null;
081: if (ra != null) {
082: ra.stop();
083: ra = null;
084: }
085: super .tearDown();
086: }
087:
088: public void testProxy() throws Exception {
089: // create reciever object
090: JmsProxyTest.TestObject testObject = new JmsProxyTest.TestObject(
091: "foo");
092: MdbInvoker mdbInvoker = new MdbInvoker(connectionFactory,
093: testObject);
094:
095: // Create a Session
096: Connection connection = connectionFactory.createConnection();
097: connection.start();
098: Session session = connection.createSession(false,
099: Session.AUTO_ACKNOWLEDGE);
100:
101: // Create the request Queue
102: Destination requestQueue = session
103: .createQueue(REQUEST_QUEUE_NAME);
104: MessageConsumer consumer = session.createConsumer(requestQueue);
105: consumer.setMessageListener(mdbInvoker);
106:
107: // create in invoker
108: JmsProxyTest.TestInterface testInterface = MdbProxy
109: .newProxyInstance(JmsProxyTest.TestInterface.class,
110: connectionFactory, REQUEST_QUEUE_NAME);
111: assertEquals("foobar", testInterface.echo("bar"));
112: assertEquals("foobar", testInterface.echo("bar"));
113: assertEquals("foobar", testInterface.echo("bar"));
114: assertEquals("foobar", testInterface.echo("bar"));
115: assertEquals("foobar", testInterface.echo("bar"));
116: }
117:
118: public static interface TestInterface {
119: String echo(String msg);
120: }
121:
122: public static class TestObject implements
123: JmsProxyTest.TestInterface {
124: private final String prefix;
125:
126: public TestObject(String prefix) {
127: this .prefix = prefix;
128: }
129:
130: public String echo(String msg) {
131: return prefix + msg;
132: }
133: }
134: }
|