[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openejb-user
Subject:    Re: OpenEJB on Oracle
From:       David Blevins <david.blevins () visi ! com>
Date:       2008-12-29 2:09:22
Message-ID: 7D692EE2-5950-4D6D-A81C-C2C7907CC167 () visi ! com
[Download RAW message or body]


On Dec 28, 2008, at 2:12 PM, JensToerber wrote:

>
> Hi again,

Hi Jens!

> I did not manage to run OpenEJB on Oracle in JUnit-Test if i  
> configure it
> like this:
> p.put(DATASOURCE, "new://Resource?type=DataSource");
>        p.put(DATASOURCE + ".JdbcDriver", "oracle.jdbc.OracleDriver");
>        // it is not working thin driver is complaining about missing
> username and/or password
>        // p.put(DATASOURCE + ".JdbcUrl", "jdbc:oracle:thin:" +  
> USERNAME +
> "/" + PASSWORD + "@192.168.2.96:1521:ORCL");
>        p.put(DATASOURCE + ".JdbcUrl", "jdbc:oracle:thin:" + USERNAME  
> + "/"
> + PASSWORD + "@192.168.2.99:1521:XE");
>        p.put("oracle.jdbc.user", USERNAME);
>        p.put("oracle.jdbc.username", USERNAME);
>        p.put("oracle.jdbc.password", PASSWORD);
>        p.put("user", "openejb");
>        p.put("password", "openejb");
>        p.put(DATASOURCE + ".JdbcUsername", "openejb");
>        p.put(DATASOURCE + ".JdbcPassword", "openejb");
>        // openejb always sets the username to sa and password to ""

You'll want to specify the "username" and "password" properties as  
follows:

         p.put("Oracle", "new://Resource?type=DataSource");
         p.put("Oracle.JdbcDriver", "oracle.jdbc.OracleDriver");
         p.put("Oracle.JdbcUrl", "jdbc:oracle:thin: 
192.168.2.99:1521:XE");
         p.put("Oracle.Username", "foo");
         p.put("Oracle.Password", "bar");

Or if you want to use a "DATASOURCE" variable as you showed in your  
code then like this:

         String DATASOURCE = "Oracle";
         String USERNAME = "foo";
         String PASSWORD = "bar";

         p.put(DATASOURCE, "new://Resource?type=DataSource");
         p.put(DATASOURCE + ".JdbcDriver", "oracle.jdbc.OracleDriver");
         p.put(DATASOURCE + ".JdbcUrl", "jdbc:oracle:thin: 
192.168.2.99:1521:XE");
         p.put(DATASOURCE + ".Username", USERNAME);
         p.put(DATASOURCE + ".Password", PASSWORD);

Note that all properties are not case-sensitive, so "username" and  
"password" will work as will "UsErNaMe" and "pAsSwOrD" and so on.

With the way you have it configured, you should see these four  
warnings in your log file saying that your guesses are not correct:

     WARN - Property "JdbcUsername" not supported by "Oracle"
     WARN - Property "jdbc.username" not supported by "Oracle"
     WARN - Property "JdbcPassword" not supported by "Oracle"
     WARN - Property "jdbc.user" not supported by "Oracle"
     WARN - Property "jdbc.password" not supported by "Oracle"

> Then i found something here to configure OpenEJB in JUnit-Tests via
> openejb.xml:
> Properties properties = getProperties();
> 	
> 	        URL config =
> this.getClass().getClassLoader().getResource("META-INF/openejb.xml");
> 	        properties.setProperty("openejb.configuration",
> config.toExternalForm());
> 	
> 	        Context retContext = null;
> 	
> 	        try {
> 				retContext = new InitialContext(properties);
>
> 	       ...
>
> Then Oracle Datasource is working in JUnit-Tests.
> Fine.

That's also a fine way to configure things for a test case.

> Got Timer running in OpenEJB JUnit-Test. It's straight forward.

Great!

> I tried to get an MDB running. This is working now in my JBoss  
> (without
> OpenEJB), not yet tested in Tomcat with OpenEJB, but it is not  
> working in
> OpenEJB-JUnit-Test.
>
> Initialization is working, OpenEJB does not complain about missing  
> Queue in
> configuration.
>
> I don't know if it should work in JUnit-Test.
> Here my inspection code:
> obj = retContext.lookup("<queue>");
> 				if (obj instanceof ActiveMQQueue) {
> 					ActiveMQQueue activeMQQueue  = (ActiveMQQueue) obj;
> 					
> 					String physicalName = activeMQQueue.getPhysicalName(); // value  
> of
> destination of openejb.xml - <Resource ...>destination ...</resource
> 					log.debug("physicalName: " + physicalName);
> 					String qualifiedName = activeMQQueue.getQualifiedName(); //
> queue://<physicalName>
> 					log.debug("qualifiedName: " + qualifiedName);					
> 					String destinationTypeAsString =
> activeMQQueue.getDestinationTypeAsString(); // Queue
> 					log.debug("destinationTypeAsString: " +  
> destinationTypeAsString);					
> 					// may throw JMSException:
> 					String queueName = activeMQQueue.getQueueName(); // equals to
> <physicalName> (?)
> 					log.debug("queueName: " + queueName);				
> 					Map options = activeMQQueue.getOptions(); // currently null
> 					log.debug("options: " + options);				
> 					Properties ps = activeMQQueue.getProperties();
> 					logProperties(ps);
> 					log.debug("ps: " + ps);				
> 				}
>
> I'll add my current openejb.xml file (for JUnit-Test).

Currently, you cannot lookup topics and queues directly inside a test  
case, you need to have an EJB (or servlet when in tomcat) lookup the  
topic or queue and pass it back.

This can be quite easy, however, by just including a bean as an inner  
class that declares dependencies on the resources you need.  For  
example:

public class MessagingTest extends TestCase {

     public void test() throws Exception {
         Properties p = new Properties();
         p.put(Context.INITIAL_CONTEXT_FACTORY,  
"org.apache.openejb.client.LocalInitialContextFactory");
         InitialContext context = new InitialContext(p);

         TestClient bean = (TestClient)  
context.lookup("TestClientBeanLocal");

         ConnectionFactory connectionFactory =  
bean.getConnectionFactory();

         Queue questionQueue = bean.getQueue();

         Connection connection = null;
         Session session = null;

         try {
             connection = connectionFactory.createConnection();
             connection.start();

             // Create a Session
             session = connection.createSession(false,  
Session.AUTO_ACKNOWLEDGE);

             // Create a MessageProducer from the Session to the Topic  
or Queue
             MessageProducer producer =  
session.createProducer(questionQueue);
             producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

             // Create a message
             TextMessage message = session.createTextMessage("Hello  
World");

             // Tell the producer to send the message
             producer.send(message);
         } finally {
             // Clean up
             if (session != null) session.close();
             if (connection != null) connection.close();
         }

     }

     @Stateless
     public static class TestClientBean implements TestClient {

         @Resource
         private ConnectionFactory connectionFactory;

         @Resource(name = "FooQueue")
         private Queue queue;

         public ConnectionFactory getConnectionFactory() {
             return connectionFactory;
         }

         public Queue getQueue() {
             return queue;
         }
     }

     public interface TestClient {

         public ConnectionFactory getConnectionFactory();

         public Queue getQueue();
     }
}

For this to work you'll need to add a META-INF/ejb-jar.xml to your  
test sources directory (src/test/resources/ for Maven).  We'll then  
scrape your test classes for ejbs as well and process them with your  
other applications.  Quite a lot of interesting testing techniques can  
be done this way.


> Currenlty i am not quite sure how to configure an ActiveMQ  
> persistent Queue
> in OpenEJB.
>
> ActiveMQ creates the Destination on the fly. In JBoss you have to  
> configure
> Queues in advance to be persistent. Non-persistent are created on  
> the fly.
>
> There is a service-jar.xml in openejb.war\org.apache.openejb.tomcat 
> \META-INF
> or openejb.war\WEB-INF\classes\META-INF\org.apache.openejb.tomcat 
> \META-INF.

The service-jar.xml is there to specify the default values for  
properties that haven't been explicitly configured the user.  We use a  
different service-jar.xml for unit testing that we do for usage in  
Tomcat, for example.  In unit testing the service-jar.xml sets up a  
profile that by default will not open any ports or consume any disk  
(i.e. no embedded database files and no persistence message stores).   
In Tomcat the service-jar.xml sets up a profile that by default will  
open ports for things like the message broker and also use disk for  
persistence message stores.

But as mentioned, this only affects the defaults when not specified.   
In all cases you can set things up explicitly how you would like  
them.  Here's how you would setup ActiveMQ for more of an online mode:

<Resource id="MyJMSResourceAdapter" type="ActiveMQResourceAdapter">
   # Broker configuration URI as defined by ActiveMQ
   # see http://activemq.apache.org/broker-configuration-uri.html
   BrokerXmlConfig = broker:(tcp://localhost:61616)?useJmx=false

   # Broker address
   ServerUrl = tcp://localhost:61616

   # DataSource for persistence messages, should be the "id" of a  
<Resource> of type DataSource
   DataSource = MyDataSource
</Resource>


Here's a more offline mode which we typically recommend for test cases  
as all clients are in the same VM.  No ports will be opened with this  
configuration:

<Resource id="MyJMSResourceAdapter" type="ActiveMQResourceAdapter">
   # Broker configuration URI as defined by ActiveMQ
   # see http://activemq.apache.org/broker-configuration-uri.html
   BrokerXmlConfig = broker:()/localhost?persistent=false

   # Broker address
   ServerUrl = vm://localhost?async=true

   # DataSource for persistence messages, should be the "id" of a  
<Resource> of type DataSource
   DataSource = MyDataSource
</Resource>


-David

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic