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.tasklist;
018:
019: import java.util.Enumeration;
020:
021: import javax.jms.Message;
022: import javax.jms.MessageConsumer;
023: import javax.jms.MessageProducer;
024: import javax.jms.Queue;
025: import javax.jms.QueueBrowser;
026: import javax.jms.Session;
027:
028: import junit.framework.TestCase;
029:
030: import org.apache.activemq.ActiveMQConnectionFactory;
031: import org.apache.activemq.xbean.BrokerFactoryBean;
032: import org.apache.log4j.BasicConfigurator;
033: import org.bpmscript.jms.JmsTemplate;
034: import org.bpmscript.jms.JmsTemplateException;
035: import org.bpmscript.jms.SessionCallback;
036: import org.springframework.core.io.ClassPathResource;
037:
038: public class JmsBrowseTransactionTest extends TestCase {
039:
040: private final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
041: .getLog(JmsBrowseTransactionTest.class);
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: protected void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: public void testSomething() throws Exception {
052: BasicConfigurator.configure();
053: BrokerFactoryBean bean = new BrokerFactoryBean();
054: bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
055: bean.afterPropertiesSet();
056:
057: final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
058: final JmsTemplate template = new JmsTemplate(connectionFactory);
059: template.afterPropertiesSet();
060:
061: final Queue queue = template
062: .getQueue(this .getClass().getName());
063:
064: try {
065: template.executeTransacted(new SessionCallback() {
066:
067: public Object doInJms(Session session) throws Exception {
068: MessageProducer producer = session
069: .createProducer(queue);
070: Message message = session.createMessage();
071: message.setStringProperty("id", "id");
072: producer.send(message);
073: return null;
074: }
075:
076: });
077: } catch (JmsTemplateException e) {
078: log.error(e, e);
079: }
080:
081: try {
082: Thread thread1 = new Thread(new Runnable() {
083:
084: public void run() {
085: try {
086: template
087: .executeTransacted(new SessionCallback() {
088:
089: public Object doInJms(
090: Session session)
091: throws Exception {
092: MessageConsumer consumer = session
093: .createConsumer(queue,
094: "id = 'id'");
095: Message message = consumer
096: .receive();
097: log.info("got message");
098: Thread.sleep(400);
099: return null;
100: }
101:
102: });
103: } catch (JmsTemplateException e) {
104: log.error(e, e);
105: }
106: }
107:
108: });
109: Thread thread2 = new Thread(new Runnable() {
110:
111: public void run() {
112: try {
113: template
114: .executeTransacted(new SessionCallback() {
115:
116: public Object doInJms(
117: Session session)
118: throws Exception {
119: Thread.sleep(200);
120: log
121: .info("browsing for message");
122: QueueBrowser browser = session
123: .createBrowser(queue);
124: Enumeration enumeration = browser
125: .getEnumeration();
126: assertTrue(enumeration
127: .hasMoreElements());
128: Message message = (Message) enumeration
129: .nextElement();
130: assertEquals(
131: "id",
132: message
133: .getStringProperty("id"));
134: log.info("done browsing");
135: return null;
136: }
137:
138: });
139: } catch (JmsTemplateException e) {
140: log.error(e, e);
141: }
142: }
143:
144: });
145: thread1.start();
146: thread2.start();
147: Thread.sleep(1000);
148: } finally {
149: template.destroy();
150: bean.destroy();
151: }
152: }
153:
154: }
|