JmsTest.java :  » Workflow-Engines » bpmscript » org » bpmscript » jms » Java Open Source

Java Open Source » Workflow Engines » bpmscript 
bpmscript » org » bpmscript » jms » JmsTest.java
/*
 * 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.bpmscript.jms;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;

import junit.framework.TestCase;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.xbean.BrokerFactoryBean;
import org.apache.log4j.BasicConfigurator;
import org.springframework.core.io.ClassPathResource;

public class JmsTest extends TestCase {

  protected void setUp() throws Exception {
    super.setUp();
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }
  
  public void testSomething() throws Exception {
    BasicConfigurator.configure();
    BrokerFactoryBean bean = new BrokerFactoryBean();
    bean.setConfig(new ClassPathResource("inmemoryactivemq.xml"));
    bean.afterPropertiesSet();
    
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    Connection connection = connectionFactory.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("org.bpmscript.jbi.tasklist.JmsTest");
    MessageProducer producer = session.createProducer(queue);

    try {
      ObjectMessage message = session.createObjectMessage("Hello World!");
      message.setStringProperty("id", "something");
      producer.send(message);
      QueueBrowser taskBrowser = session.createBrowser(queue, "id = 'something'");
      Enumeration enumeration = taskBrowser.getEnumeration();
      assertTrue(enumeration.hasMoreElements());
      QueueBrowser browser = session.createBrowser(queue);
      Enumeration e = browser.getEnumeration();
      assertTrue(e.hasMoreElements());
    } finally {
      MessageConsumer consumer = session.createConsumer(queue);
      while(consumer.receiveNoWait() != null) { }
    }
    
    session.close();
    connection.stop();
    connection.close();

    bean.destroy();
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.