简体   繁体   中英

How to use embedded openejb from a java SE application?

I am writing a little java library that is intended to be used in a web-application as well as by a java console-application.

In order to profit from CDI and other javaEE 6 features and not having to maintain two versions (java EE and java SE) of the library I'd like to use openejb (embedded) for the console-application. So I've built a maven project in eclipse and added the openejb artifact.

Somehow I just don't get how to make the console program use the openejb-container, that is resolve my injections and other javaEE features.

Lets say I have two very simple classes:

@Stateless
Class A {

    @Inject
    public B member;

    public A() {};

}

and

@Stateless
Class B {

    public B() {};

    public String getString () {
        return "Hello";

    }

}

So, how would I get a plain old java class with a main() method make instantiate a member of A using the embedded openejb? - in a way like:

public class TestOpenEJB {

    public static void main(String[] args) {

        Class A a = new A(); /*wrong of couse*/

        System.out.println( a.member.getString() );

    }
}

A working solution for this simple example would be helpful.

Finally, my aim here is to provide a java SE api for a library that uses an embedded javaEE container internally.

Thanks a lot!

Additional to my comments, I think your problem can be answered in this way:

Go on and model your library's behaviour with EJBs (as shown in your code example). This is a good approach, since the container cares about things like pooling, parallel access, transactions and such.

Then your web application (assuming it's in the same container) can just use those EJBs directly.

And for accessing it via a console application, you can either run it within an application client container (which is preferable than trying to embed a container in your application), or (which I would recommend) expose your business logic in an additional way (eg via REST) and use that in a standalone client application.

PS: For integration testing your business logic with DI mechanisms, use Arquillian .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM