diff options
| author | Kim van der Riet <kpvdr@apache.org> | 2012-05-04 15:39:19 +0000 |
|---|---|---|
| committer | Kim van der Riet <kpvdr@apache.org> | 2012-05-04 15:39:19 +0000 |
| commit | 633c33f224f3196f3f9bd80bd2e418d8143fea06 (patch) | |
| tree | 1391da89470593209466df68c0b40b89c14963b1 /java/jca/example/src/main | |
| parent | c73f9286ebff93a6c8dbc29cf05e258c4b55c976 (diff) | |
| download | qpid-python-633c33f224f3196f3f9bd80bd2e418d8143fea06.tar.gz | |
QPID-3858: Updated branch - merged from trunk r.1333987
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/asyncstore@1334037 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/jca/example/src/main')
9 files changed, 343 insertions, 20 deletions
diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/client/QpidRequestResponseClient.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/client/QpidRequestResponseClient.java index 734df1c0f3..fd5b3efff0 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/client/QpidRequestResponseClient.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/client/QpidRequestResponseClient.java @@ -42,7 +42,7 @@ public class QpidRequestResponseClient implements MessageListener, Runnable private static final Logger _log = LoggerFactory.getLogger(QpidRequestResponseClient.class); private static final String DEFAULT_CF_JNDI = "QpidConnectionFactory"; - private static final String DEFAULT_DESTINATION_JNDI = "QpidResponderQueue"; + private static final String DEFAULT_DESTINATION_JNDI = "QpidRequestQueue"; private static final String DEFAULT_MESSAGE = "Hello, World!"; private static final int DEFAULT_MESSAGE_COUNT = 1; private static final int DEFAULT_THREAD_COUNT = 1; diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeListenerBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeListenerBean.java index 9cf220de2a..bdb722a87b 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeListenerBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeListenerBean.java @@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), - @ActivationConfigProperty(propertyName = "destination", propertyValue = "@qpid.goodbye.queue.jndi.name@"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "@jndi.prefix@@qpid.goodbye.queue.jndi.name@"), @ActivationConfigProperty(propertyName = "connectionURL", propertyValue = "@broker.url@"), @ActivationConfigProperty(propertyName = "useLocalTx", propertyValue = "false"), @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeSubscriberBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeSubscriberBean.java index 64e0effb1f..f2e4d6aaa5 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeSubscriberBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidGoodByeSubscriberBean.java @@ -34,10 +34,9 @@ import org.slf4j.LoggerFactory; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), - @ActivationConfigProperty(propertyName = "destination", propertyValue = "@qpid.goodbye.topic.jndi.name@"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "@jndi.prefix@@qpid.goodbye.topic.jndi.name@"), @ActivationConfigProperty(propertyName = "connectionURL", propertyValue = "@broker.url@"), @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "NotDurable"), - @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "hello.Topic"), @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") }) diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloListenerBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloListenerBean.java index 0056e7b0b8..75e0acab79 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloListenerBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloListenerBean.java @@ -42,7 +42,7 @@ import org.slf4j.LoggerFactory; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), - @ActivationConfigProperty(propertyName = "destination", propertyValue = "@qpid.hello.queue.jndi.name@"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "@jndi.prefix@@qpid.hello.queue.jndi.name@"), @ActivationConfigProperty(propertyName = "connectionURL", propertyValue = "@broker.url@"), @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") }) @@ -53,7 +53,7 @@ public class QpidHelloListenerBean implements MessageListener @Resource(@jndi.scheme@="@qpid.xacf.jndi.name@") private ConnectionFactory _connectionFactory; - @Resource(@jndi.scheme@="GoodByeQueue") + @Resource(@jndi.scheme@="@qpid.goodbye.queue.jndi.name@") private Destination _queue; @Override diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloSubscriberBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloSubscriberBean.java index 560de36e48..882c85fa18 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloSubscriberBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidHelloSubscriberBean.java @@ -42,10 +42,9 @@ import org.slf4j.LoggerFactory; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), - @ActivationConfigProperty(propertyName = "destination", propertyValue = "@qpid.hello.topic.jndi.name@"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "@jndi.prefix@@qpid.hello.topic.jndi.name@"), @ActivationConfigProperty(propertyName = "connectionURL", propertyValue = "@broker.url@"), @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "NotDurable"), - @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "hello.Topic"), @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") }) public class QpidHelloSubscriberBean implements MessageListener @@ -55,7 +54,7 @@ public class QpidHelloSubscriberBean implements MessageListener @Resource(@jndi.scheme@="@qpid.xacf.jndi.name@") private ConnectionFactory _connectionFactory; - @Resource(@jndi.scheme@="GoodByeTopic") + @Resource(@jndi.scheme@="@qpid.goodbye.topic.jndi.name@") private Destination _topic; @Override diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidJMSResponderBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidJMSResponderBean.java index e7b44e10ca..6e99d4fe7f 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidJMSResponderBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidJMSResponderBean.java @@ -27,6 +27,7 @@ import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.Connection; import javax.jms.ConnectionFactory; +import javax.jms.Destination; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.MessageProducer; @@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), - @ActivationConfigProperty(propertyName = "destination", propertyValue = "@qpid.responder.queue.jndi.name@"), + @ActivationConfigProperty(propertyName = "destination", propertyValue = "@jndi.prefix@@qpid.request.queue.jndi.name@"), @ActivationConfigProperty(propertyName = "connectionURL", propertyValue = "@broker.url@"), @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") }) @@ -72,10 +73,12 @@ public class QpidJMSResponderBean implements MessageListener temp.append("QpidJMSResponderBean received message with content: [" + content); temp.append("] at " + new Date()); + connection = _connectionFactory.createConnection(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + if(message.getJMSReplyTo() != null) { - connection = _connectionFactory.createConnection(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + _log.info("Sending response via JMSReplyTo"); messageProducer = session.createProducer(message.getJMSReplyTo()); response = session.createTextMessage(); response.setText(temp.toString()); @@ -83,8 +86,10 @@ public class QpidJMSResponderBean implements MessageListener } else { - _log.warn("Response was requested with no JMSReplyToDestination set. Will not respond to message."); + _log.info("JMSReplyTo is null. Will not respond to message."); } + + } } catch(Exception e) diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidTestBean.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidTestBean.java index 014b459699..a5f4770257 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidTestBean.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/ejb/QpidTestBean.java @@ -43,10 +43,10 @@ public class QpidTestBean implements QpidTestRemote, QpidTestLocal @Resource(@jndi.scheme@="@qpid.xacf.jndi.name@") private ConnectionFactory _connectionFactory; - @Resource(@jndi.scheme@="HelloQueue") + @Resource(@jndi.scheme@="@qpid.hello.queue.jndi.name@") private Destination _queue; - @Resource(@jndi.scheme@="HelloTopic") + @Resource(@jndi.scheme@="@qpid.hello.topic.jndi.name@") private Destination _topic; @Override diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidRequestResponseServlet.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidRequestResponseServlet.java new file mode 100644 index 0000000000..d069a0c943 --- /dev/null +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidRequestResponseServlet.java @@ -0,0 +1,283 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.jca.example.web; +import java.io.IOException; +import java.lang.Thread; + +import javax.annotation.Resource; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.InitialContext; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.transaction.UserTransaction; + +import org.apache.qpid.jca.example.ejb.QpidTest; +import org.apache.qpid.jca.example.ejb.QpidUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@SuppressWarnings("serial") +public class QpidRequestResponseServlet extends HttpServlet +{ + private static final Logger _log = LoggerFactory.getLogger(QpidTestServlet.class); + + private static final String DEFAULT_MESSAGE = "Hello, World!"; + private static final int DEFAULT_COUNT = 1; + private static final boolean DEFAULT_XA = false; + private static final boolean DEFAULT_TX = false; + private static final boolean USE_TMP_QUEUE = false; + + @Resource(@jndi.scheme@="@qpid.xacf.jndi.name@") + private ConnectionFactory _connectionFactory; + + @Resource(@jndi.scheme@="@qpid.request.queue.jndi.name@") + private Destination _queue; + + @Resource(@jndi.scheme@="@qpid.response.queue.jndi.name@") + private Destination _responseQueue; + + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + doPost(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + InitialContext ctx = null; + Connection connection = null; + Session session = null; + MessageProducer messageProducer = null; + UserTransaction ut = null; + boolean useXA = false; + boolean rollback = false; + boolean useTx = false; + MessageConsumer messageConsumer = null; + long startTime = 0; + + try + { + String content = (req.getParameter("message") == null) ? DEFAULT_MESSAGE : req.getParameter("message"); + int count = (req.getParameter("count") == null) ? DEFAULT_COUNT : Integer.valueOf(req.getParameter("count")); + useXA = (req.getParameter("useXA") == null) ? DEFAULT_XA : Boolean.valueOf(req.getParameter("useXA")); + useTx = (req.getParameter("useTx") == null) ? DEFAULT_TX : Boolean.valueOf(req.getParameter("useTx")); + + ctx = new InitialContext(); + + _log.debug("Environment: "); + _log.debug("Message content: " + content); + _log.debug("Message count:" + count); + _log.debug("Using XA: " + useXA); + + resp.getOutputStream().println("Environment: "); + resp.getOutputStream().println("Message content: " + content); + resp.getOutputStream().println("Message count:" + count); + resp.getOutputStream().println("Using XA: " + useXA); + + try + { + + connection = _connectionFactory.createConnection(); + + if(useXA) + { + ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction"); + ut.begin(); + useTx = false; + } + + session = (useXA) ? connection.createSession(false, Session.AUTO_ACKNOWLEDGE) : connection.createSession(useTx, Session.AUTO_ACKNOWLEDGE); + messageProducer = session.createProducer(_queue); + + startTime = System.currentTimeMillis(); + + for(int i = 0; i < count; i++) + { + TextMessage message = session.createTextMessage(content); + message.setJMSReplyTo(_responseQueue); + messageProducer.send(message); + } + + } + catch(Exception e) + { + rollback = true; + + if(useXA && ut != null) + { + try + { + ut.setRollbackOnly(); + } + catch(Exception ex) + { + _log.error(ex.getMessage(), ex); + throw new ServletException(ex.getMessage(), ex); + } + } + } + finally + { + try + { + if(useXA && ut != null) + { + if(rollback) + { + ut.rollback(); + } + else + { + ut.commit(); + } + } + if(useTx && !useXA) + { + if(rollback) + { + session.rollback(); + } + else + { + session.commit(); + } + } + } + catch(Exception e) + { + _log.error(e.getMessage(), e); + throw new ServletException(e.getMessage(), e); + } + + QpidUtil.closeResources(messageProducer, session); + } + + resp.getOutputStream().println("Sent " + count + " messages with content '" + content + "'"); + resp.getOutputStream().flush(); + + int ackMode = Session.AUTO_ACKNOWLEDGE; + rollback = false; + + if(useXA) + { + ut.begin(); + } + + session = (useXA) ? connection.createSession(false, Session.AUTO_ACKNOWLEDGE) : connection.createSession(useTx, Session.AUTO_ACKNOWLEDGE); + messageConsumer = session.createConsumer(_responseQueue); + connection.start(); + + for(int i = 0; i < count; i++) + { + TextMessage message = (TextMessage)messageConsumer.receive(5000); + + if(message != null) + { + message.acknowledge(); + content = message.getText(); + + } + } + + startTime = System.currentTimeMillis() - startTime; + resp.getOutputStream().println("Received " + count + " messages with content '" + content + "'"); + resp.getOutputStream().println("Total process time " + startTime); + } + catch(Exception e) + { + rollback = true; + + if(useXA && ut != null) + { + try + { + ut.setRollbackOnly(); + } + catch(Exception ex) + { + _log.error(ex.getMessage(), ex); + throw new ServletException(ex.getMessage(), ex); + } + } + } + finally + { + if(useXA && ut != null) + { + try + { + if(rollback) + { + ut.rollback(); + } + else + { + ut.commit(); + } + } + catch(Exception e) + { + _log.error(e.getMessage(), e); + throw new ServletException(e.getMessage(), e); + + } + } + + if(useTx && !useXA) + { + try + { + if(rollback) + { + session.rollback(); + } + else + { + session.commit(); + } + } + catch(Exception e) + { + _log.error(e.getMessage(), e); + throw new ServletException(e.getMessage(), e); + } + } + + QpidUtil.closeResources(messageProducer, session); + } + } + +} + + diff --git a/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidTestServlet.java b/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidTestServlet.java index 11a61e762c..7526daa83d 100644 --- a/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidTestServlet.java +++ b/java/jca/example/src/main/java/org/apache/qpid/jca/example/web/QpidTestServlet.java @@ -52,15 +52,16 @@ public class QpidTestServlet extends HttpServlet private static final int DEFAULT_COUNT = 1; private static final boolean DEFAULT_TOPIC = false; private static final boolean DEFAULT_XA = false; + private static final boolean DEFAULT_TX = false; private static final boolean DEFAULT_SAY_GOODBYE = true; @Resource(@jndi.scheme@="@qpid.xacf.jndi.name@") private ConnectionFactory _connectionFactory; - @Resource(@jndi.scheme@="HelloQueue") + @Resource(@jndi.scheme@="@qpid.hello.queue.jndi.name@") private Destination _queue; - @Resource(@jndi.scheme@="HelloTopic") + @Resource(@jndi.scheme@="@qpid.hello.topic.jndi.name@") private Destination _topic; @EJB @@ -82,6 +83,7 @@ public class QpidTestServlet extends HttpServlet UserTransaction ut = null; boolean useXA = false; boolean rollback = false; + boolean useTX = false; try { @@ -90,8 +92,10 @@ public class QpidTestServlet extends HttpServlet int count = (req.getParameter("count") == null) ? DEFAULT_COUNT : Integer.valueOf(req.getParameter("count")); boolean useTopic = (req.getParameter("useTopic") == null) ? DEFAULT_TOPIC : Boolean.valueOf(req.getParameter("useTopic")); useXA = (req.getParameter("useXA") == null) ? DEFAULT_XA : Boolean.valueOf(req.getParameter("useXA")); + useTX = (req.getParameter("useTX") == null) ? DEFAULT_TX : Boolean.valueOf(req.getParameter("useTX")); ctx = new InitialContext(); boolean sayGoodBye = (req.getParameter("sayGoodBye") == null) ? DEFAULT_SAY_GOODBYE : Boolean.valueOf(req.getParameter("sayGoodBye")); + useTX = (req.getParameter("useTX") == null) ? DEFAULT_TOPIC : Boolean.valueOf(req.getParameter("DEFAULT_TX")); _log.debug("Environment: "); _log.debug("Message content: " + content); @@ -122,7 +126,7 @@ public class QpidTestServlet extends HttpServlet } connection = _connectionFactory.createConnection(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + session = connection.createSession(useTX, Session.AUTO_ACKNOWLEDGE); messageProducer = (useTopic) ? session.createProducer(_topic) : session.createProducer(_queue); for(int i = 0; i < count; i++) @@ -155,6 +159,19 @@ public class QpidTestServlet extends HttpServlet } } + if(useTX) + { + try + { + session.rollback(); + } + catch(Exception ex) + { + _log.error(ex.getMessage(), ex); + throw new ServletException(ex.getMessage(), ex); + } + } + _log.error(e.getMessage(), e); throw new ServletException(e.getMessage(), e); } @@ -181,12 +198,32 @@ public class QpidTestServlet extends HttpServlet } } + if(useTX && !useXA) + { + try + { + + if(rollback) + { + session.rollback(); + } + else + { + session.commit(); + } + } + catch(Exception e) + { + + _log.error(e.getMessage(), e); + throw new ServletException(e.getMessage(), e); + } + } + QpidUtil.closeResources(session, connection, ctx); } } - - } |
