You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Andy Gumbrecht <an...@orprovision.com> on 2009/10/07 16:45:32 UTC

Multiple instances of OpenEJB

Hi all,

Is it possible to run multiple instances of OpenEJB in the same jvm?

Something like this...

JVM{

     Session1{
         OpenEJB1{Persistence-unit(bob - points to DB 1)}
     }

     Session2{
          OpenEJB2{Persistence-unit(bob - but this points to DB 2)}
      }

}

Thanks in advance,
-- 
------------------------------------------------------------------------

*Andy Gumbrecht*
Software Developer
Orpro Vision GmbH
Hefehof 8, 31785, Hameln

Tel +49 (0) 5151 809 44 21
Cell +49 (0) 174 1800 381
Email andy.gumbrecht@orprovision.com
Web www.orprovision.com


            Orpro Vision GmbH
            Sitz der Gesellschaft: 31785, Hameln
            USt-Id-Nr: DE264453214
            Amtsgericht Hannover HRB204336
            Geschaeftsfuehrer: Roberto Gatti

------------------------------------------------------------------------


            Diese E-Mail enthält vertrauliche und/oder rechtlich
            geschützte Informationen. Wenn Sie nicht der richtige
            Adressat sind oder diese E-Mail irrtümlich erhalten haben,
            informieren Sie bitte sofort den Absender und vernichten Sie
            diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
            Verwendung sowie die unbefugte Weitergabe dieser Mail ist
            nicht gestattet.

------------------------------------------------------------------------


            This e-mail may contain confidential and/or privileged
            information. If you are not the intended recipient (or have
            received this e-mail in error) please notify the sender
            immediately and destroy this e-mail. Any unauthorized
            copying, disclosure, distribution or other use of the
            material or parts thereof is strictly forbidden.

------------------------------------------------------------------------



Dynamic datasources

Posted by Andy Gumbrecht <an...@orprovision.com>.
Hi all,

Just thought I should share this snippet for creating DataSource 
resources either before or after an embedded OpenEJB has started.
Thanks go out to David Blevins for pointing me in the right direction.

The method can be fed a non-null environment (env) properties which will 
be seeded before use in the InitialContext call.
If the 'env' parameter is null then it is assumed that an InitialContext 
has been created for OpenEJB, in which case a live Resource is created.

Obviously this is quite specific to my requirements, but can be easily 
modified.


private static final Log log = LogFactory.getLog(Session.class);
private static final Set<String> _dataSources = new HashSet<String>();

     /**
      * Creates a DataSource resource for OpenEJB.
      * @param env If not null the properties are seeds with parameters 
to create the DataSource .
      * @param managed Is this a JTA managed or unmanaged DataSource.
      * @param f File location for the database - Is specific for file 
based databases such as Derby, etc.
      * @param name DataSource name - Will prepend to either 
[name]Managed or [name]Unmanaged.
      * @throws Exception If anything goes wrong.
      */
private static void addDataSourceConfig(Properties env, final boolean 
managed, final File f, final String name) throws Exception {

         final String m = (managed ? "Managed" : "Unmanaged");

         if (_dataSources.contains(String.format("%1$s%2$s", name, m))) {
             //Already configured for OpenEJB
             return;
         }

         final Resource resource = (null == env ? new 
Resource(String.format("%1$s%2$s", name, m), "DataSource") : null);
         final String dsname;

         if (null != resource) {
             env = resource.getProperties();
             dsname = "";
         } else {
             env.put(String.format("%1$s%2$s", name, m), 
"new://Resource?type=DataSource");
             dsname = String.format("%1$s%2$s.", name, m);
         }

         env.put(String.format("%1$sJtaManaged", dsname), (managed ? 
"true" : "false"));
         env.put(String.format("%1$sUserName", dsname), "SA");
         env.put(String.format("%1$sJdbcDriver", dsname), 
"org.apache.derby.jdbc.EmbeddedDriver");
         env.put(String.format("%1$sJdbcUrl", dsname), 
String.format("jdbc:derby:%1$s/%2$s;create=true", 
f.getPath().replace("\\", "/"), m.toLowerCase()));

         if (null != resource) {

             try {
                 
SystemInstance.get().getComponent(Assembler.class).createResource(new 
ConfigurationFactory().configureService(resource, ResourceInfo.class));
             } catch (OpenEJBException e) {
                 log.error("error:", e);
                 throw new Exception(String.format("Failed to add 
DataSource %1$s%2$s", name, m), e);
             }
         }

         _dataSources.add(String.format("%1$s%2$s", name, m));
     }

Best regards,

Andy.

PS. Something I also do which might be useful to someone is to create 
and load an application dynamically after OpenEJB has started.

//Dynamically load application
  SystemInstance.get().getComponent(Assembler.class).createApplication(new ConfigurationFactory().configureApplication(
     new File("my.ejb.jar")
   ));

Re: Multiple instances of OpenEJB - New strategy

Posted by Andy Gumbrecht <an...@orprovision.com>.
Thanks David,

Just what I was looking for!

I do love the oranges and limes - for some reason I always use bob, jane 
and if I get stuck for a third sally.. I guess you could use lemons :-)

Cheers.

On 09.10.2009 07:40, David Blevins wrote:
>
> On Oct 8, 2009, at 2:11 AM, Andy Gumbrecht wrote:
>
>> However, I think that a better more lightweight option is to have one 
>> OpenEJB embedded instance that provides access to EJBs that in turn 
>> obtain EntityManagers from EntityManagerFactories bound to the 
>> corresponding PU cache determined by the call context. I would 
>> replace the @PersistenceUnit on EJBs with an @EJB helper to lookup 
>> and provide the EMs to the EJBs.
>>
>> What I would really like to do is sew in a new named persistence unit 
>> dynamically at any time 'after' start up of OpenEJB - including the 
>> dynamic DataSources.
>>
>> This leads to the new question - Is it possible to directly access 
>> OpenEJB (Assembler assembler = 
>> SystemInstance.get().getComponent(Assembler.class);) and create or 
>> define a new PU on the fly? - Using appInfo.persistenceUnits.add() 
>> for example. Is that safe?
>
> If you wanted to tinker around in the low level internal code, yes, 
> you can do some pretty crazy stuff dynamically :)  Be warned however 
> we reserve the right to change them without notice.
>
> So all disclaimers aside, if you really want to be naughty, this test 
> case is a good (er, I mean, bad, very bad, shame on you for even 
> looking) example of how things look internally when we deploy an app 
> with a persistence unit:
>
> http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.1/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java 
>
>
> In your situation you'd already have an Assembler there, so skip 
> everything we do in the setUp() of the test case with one exception.  
> You'll still need to create a 'new ConfigurationFactory();'
>
> The other option is that we do support multicast discovery in the 
> client and server.  Not sure how that might play into your goal, but 
> might open some doors if you need the client to dynamically failover 
> to another server.
>
> -David
>
>
>


-- 
------------------------------------------------------------------------

*Andy Gumbrecht*
Software Developer
Orpro Vision GmbH
Hefehof 8, 31785, Hameln

Tel +49 (0) 5151 809 44 21
Cell +49 (0) 174 1800 381
Email andy.gumbrecht@orprovision.com
Web www.orprovision.com


            Orpro Vision GmbH
            Sitz der Gesellschaft: 31785, Hameln
            USt-Id-Nr: DE264453214
            Amtsgericht Hannover HRB204336
            Geschaeftsfuehrer: Roberto Gatti

------------------------------------------------------------------------


            Diese E-Mail enthält vertrauliche und/oder rechtlich
            geschützte Informationen. Wenn Sie nicht der richtige
            Adressat sind oder diese E-Mail irrtümlich erhalten haben,
            informieren Sie bitte sofort den Absender und vernichten Sie
            diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
            Verwendung sowie die unbefugte Weitergabe dieser Mail ist
            nicht gestattet.

------------------------------------------------------------------------


            This e-mail may contain confidential and/or privileged
            information. If you are not the intended recipient (or have
            received this e-mail in error) please notify the sender
            immediately and destroy this e-mail. Any unauthorized
            copying, disclosure, distribution or other use of the
            material or parts thereof is strictly forbidden.

------------------------------------------------------------------------



Re: Multiple instances of OpenEJB - New strategy

Posted by David Blevins <da...@visi.com>.
On Oct 8, 2009, at 2:11 AM, Andy Gumbrecht wrote:

> However, I think that a better more lightweight option is to have  
> one OpenEJB embedded instance that provides access to EJBs that in  
> turn obtain EntityManagers from EntityManagerFactories bound to the  
> corresponding PU cache determined by the call context. I would  
> replace the @PersistenceUnit on EJBs with an @EJB helper to lookup  
> and provide the EMs to the EJBs.
>
> What I would really like to do is sew in a new named persistence  
> unit dynamically at any time 'after' start up of OpenEJB - including  
> the dynamic DataSources.
>
> This leads to the new question - Is it possible to directly access  
> OpenEJB (Assembler assembler =  
> SystemInstance.get().getComponent(Assembler.class);) and create or  
> define a new PU on the fly? - Using appInfo.persistenceUnits.add()  
> for example. Is that safe?

If you wanted to tinker around in the low level internal code, yes,  
you can do some pretty crazy stuff dynamically :)  Be warned however  
we reserve the right to change them without notice.

So all disclaimers aside, if you really want to be naughty, this test  
case is a good (er, I mean, bad, very bad, shame on you for even  
looking) example of how things look internally when we deploy an app  
with a persistence unit:

http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1.1/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java

In your situation you'd already have an Assembler there, so skip  
everything we do in the setUp() of the test case with one exception.   
You'll still need to create a 'new ConfigurationFactory();'

The other option is that we do support multicast discovery in the  
client and server.  Not sure how that might play into your goal, but  
might open some doors if you need the client to dynamically failover  
to another server.

-David


Re: Multiple instances of OpenEJB - New strategy

Posted by Andy Gumbrecht <an...@orprovision.com>.
Hi Quintin ,
Thanks for the interest.

I am experimenting (currently not under pressure) at the roots of a new 
persistence gateway that I am writing for a broader application, which 
leaves the first question open to a flat 'no'.

I guess I have already discounted the feasibility over night, but still 
have a scenario that opens a new topic.

First, I am using OpenEJB as an embedded entity cache (on the latest 
Derby - which is performing brilliantly I have to say!), and also as a 
local EJB gateway.

Without going into the nitty gritty, I basically (understatement) have a 
JBoss (JB) cluster that can be joined by further servers at any time 
(servers can also go off-line).
Each server in the cluster brings in it's own separate, yet identical, 
persistence unit (PU) and announces the fact to all clients through 
messaging.

1. JB1 - PU1 - Initial
2. Clients fire up...
3. JB2 - PU2 - Joins JB1 in a cluster 10 minutes later for example.
4. JBn -PUn.... etc.

JB1 to JBn drop in and deploy each others PUs so that EJBs on both JBs 
can service calls to any PU throughout the cluster.

So, now to the client. The client has started and created a 
configuration for OpenEJB based upon the current JB cluster (initially PU1).
The client creates a PU identical to the PU1 on the cluster - this is 
used locally to cache entities retrieved from the JB cluster.

3. <-- Ten minutes later PUn comes on-line and the client obtains a 
notification message .

The client cannot be restarted as it may be processing information to 
and from the PU1 cache, but it should now have to opportunity to 
redirect relevant data to and from a new local PUn cache context.

The first option I pondered over was to run a new OpenEJB process 
(either local remote or embedded) with the new local PU cache and EJB 
application to service each PU on the cluster. This led me to my initial 
question - 'run multiple instances of OpenEJB in the same jvm'

However, I think that a better more lightweight option is to have one 
OpenEJB embedded instance that provides access to EJBs that in turn 
obtain EntityManagers from EntityManagerFactories bound to the 
corresponding PU cache determined by the call context. I would replace 
the @PersistenceUnit on EJBs with an @EJB helper to lookup and provide 
the EMs to the EJBs.

What I would really like to do is sew in a new named persistence unit 
dynamically at any time 'after' start up of OpenEJB - including the 
dynamic DataSources.

This leads to the new question - Is it possible to directly access 
OpenEJB (Assembler assembler = 
SystemInstance.get().getComponent(Assembler.class);) and create or 
define a new PU on the fly? - Using appInfo.persistenceUnits.add() for 
example. Is that safe?

Best regards,

Andy.


Quintin Beukes schrieb:
> What are you trying to achieve?
>
> Quintin Beukes
>
>
>
> On Wed, Oct 7, 2009 at 4:45 PM, Andy Gumbrecht
> <an...@orprovision.com> wrote:
>   
>> Hi all,
>>
>> Is it possible to run multiple instances of OpenEJB in the same jvm?
>>
>> Something like this...
>>
>> JVM{
>>
>>    Session1{
>>        OpenEJB1{Persistence-unit(bob - points to DB 1)}
>>    }
>>
>>    Session2{
>>         OpenEJB2{Persistence-unit(bob - but this points to DB 2)}
>>     }
>>
>> }
>>
>> Thanks in advance,
>> --
>> ------------------------------------------------------------------------
>>
>> *Andy Gumbrecht*
>> Software Developer
>> Orpro Vision GmbH
>> Hefehof 8, 31785, Hameln
>>
>> Tel +49 (0) 5151 809 44 21
>> Cell +49 (0) 174 1800 381
>> Email andy.gumbrecht@orprovision.com
>> Web www.orprovision.com
>>
>>
>>           Orpro Vision GmbH
>>           Sitz der Gesellschaft: 31785, Hameln
>>           USt-Id-Nr: DE264453214
>>           Amtsgericht Hannover HRB204336
>>           Geschaeftsfuehrer: Roberto Gatti
>>
>> ------------------------------------------------------------------------
>>
>>
>>           Diese E-Mail enthält vertrauliche und/oder rechtlich
>>           geschützte Informationen. Wenn Sie nicht der richtige
>>           Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>>           informieren Sie bitte sofort den Absender und vernichten Sie
>>           diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
>>           Verwendung sowie die unbefugte Weitergabe dieser Mail ist
>>           nicht gestattet.
>>
>> ------------------------------------------------------------------------
>>
>>
>>           This e-mail may contain confidential and/or privileged
>>           information. If you are not the intended recipient (or have
>>           received this e-mail in error) please notify the sender
>>           immediately and destroy this e-mail. Any unauthorized
>>           copying, disclosure, distribution or other use of the
>>           material or parts thereof is strictly forbidden.
>>
>> ------------------------------------------------------------------------
>>
>>
>>
>>     
>
>
>   


-- 
------------------------------------------------------------------------

*Andy Gumbrecht*
Software Developer
Orpro Vision GmbH
Hefehof 8, 31785, Hameln

Tel +49 (0) 5151 809 44 21
Cell +49 (0) 174 1800 381
Email andy.gumbrecht@orprovision.com
Web www.orprovision.com


            Orpro Vision GmbH
            Sitz der Gesellschaft: 31785, Hameln
            USt-Id-Nr: DE264453214
            Amtsgericht Hannover HRB204336
            Geschaeftsfuehrer: Roberto Gatti

------------------------------------------------------------------------


            Diese E-Mail enthält vertrauliche und/oder rechtlich
            geschützte Informationen. Wenn Sie nicht der richtige
            Adressat sind oder diese E-Mail irrtümlich erhalten haben,
            informieren Sie bitte sofort den Absender und vernichten Sie
            diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
            Verwendung sowie die unbefugte Weitergabe dieser Mail ist
            nicht gestattet.

------------------------------------------------------------------------


            This e-mail may contain confidential and/or privileged
            information. If you are not the intended recipient (or have
            received this e-mail in error) please notify the sender
            immediately and destroy this e-mail. Any unauthorized
            copying, disclosure, distribution or other use of the
            material or parts thereof is strictly forbidden.

------------------------------------------------------------------------



Re: Multiple instances of OpenEJB

Posted by Quintin Beukes <qu...@skywalk.co.za>.
What are you trying to achieve?

Quintin Beukes



On Wed, Oct 7, 2009 at 4:45 PM, Andy Gumbrecht
<an...@orprovision.com> wrote:
> Hi all,
>
> Is it possible to run multiple instances of OpenEJB in the same jvm?
>
> Something like this...
>
> JVM{
>
>    Session1{
>        OpenEJB1{Persistence-unit(bob - points to DB 1)}
>    }
>
>    Session2{
>         OpenEJB2{Persistence-unit(bob - but this points to DB 2)}
>     }
>
> }
>
> Thanks in advance,
> --
> ------------------------------------------------------------------------
>
> *Andy Gumbrecht*
> Software Developer
> Orpro Vision GmbH
> Hefehof 8, 31785, Hameln
>
> Tel +49 (0) 5151 809 44 21
> Cell +49 (0) 174 1800 381
> Email andy.gumbrecht@orprovision.com
> Web www.orprovision.com
>
>
>           Orpro Vision GmbH
>           Sitz der Gesellschaft: 31785, Hameln
>           USt-Id-Nr: DE264453214
>           Amtsgericht Hannover HRB204336
>           Geschaeftsfuehrer: Roberto Gatti
>
> ------------------------------------------------------------------------
>
>
>           Diese E-Mail enthält vertrauliche und/oder rechtlich
>           geschützte Informationen. Wenn Sie nicht der richtige
>           Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>           informieren Sie bitte sofort den Absender und vernichten Sie
>           diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
>           Verwendung sowie die unbefugte Weitergabe dieser Mail ist
>           nicht gestattet.
>
> ------------------------------------------------------------------------
>
>
>           This e-mail may contain confidential and/or privileged
>           information. If you are not the intended recipient (or have
>           received this e-mail in error) please notify the sender
>           immediately and destroy this e-mail. Any unauthorized
>           copying, disclosure, distribution or other use of the
>           material or parts thereof is strictly forbidden.
>
> ------------------------------------------------------------------------
>
>
>

Re: Multiple instances of OpenEJB

Posted by Jean-Louis MONTEIRO <je...@atosorigin.com>.
Hi Andy,

No it isn't (exept may be using OSGI).
Can you give us more details about your needs ?

Jean-Louis


Andy Gumbrecht wrote:
> 
> Hi all,
> 
> Is it possible to run multiple instances of OpenEJB in the same jvm?
> 
> Something like this...
> 
> JVM{
> 
>      Session1{
>          OpenEJB1{Persistence-unit(bob - points to DB 1)}
>      }
> 
>      Session2{
>           OpenEJB2{Persistence-unit(bob - but this points to DB 2)}
>       }
> 
> }
> 
> Thanks in advance,
> -- 
> ------------------------------------------------------------------------
> 
> *Andy Gumbrecht*
> Software Developer
> Orpro Vision GmbH
> Hefehof 8, 31785, Hameln
> 
> Tel +49 (0) 5151 809 44 21
> Cell +49 (0) 174 1800 381
> Email andy.gumbrecht@orprovision.com
> Web www.orprovision.com
> 
> 
>             Orpro Vision GmbH
>             Sitz der Gesellschaft: 31785, Hameln
>             USt-Id-Nr: DE264453214
>             Amtsgericht Hannover HRB204336
>             Geschaeftsfuehrer: Roberto Gatti
> 
> ------------------------------------------------------------------------
> 
> 
>             Diese E-Mail enthält vertrauliche und/oder rechtlich
>             geschützte Informationen. Wenn Sie nicht der richtige
>             Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>             informieren Sie bitte sofort den Absender und vernichten Sie
>             diese Mail. Das unerlaubte Kopieren, jegliche anderweitige
>             Verwendung sowie die unbefugte Weitergabe dieser Mail ist
>             nicht gestattet.
> 
> ------------------------------------------------------------------------
> 
> 
>             This e-mail may contain confidential and/or privileged
>             information. If you are not the intended recipient (or have
>             received this e-mail in error) please notify the sender
>             immediately and destroy this e-mail. Any unauthorized
>             copying, disclosure, distribution or other use of the
>             material or parts thereof is strictly forbidden.
> 
> ------------------------------------------------------------------------
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Multiple-instances-of-OpenEJB-tp25788026p25799932.html
Sent from the OpenEJB User mailing list archive at Nabble.com.