简体   繁体   English

如何从Java SE应用程序使用嵌入式openejb?

[英]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. 我正在编写一个小的Java库,该库旨在用于Web应用程序以及Java控制台应用程序。

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. 为了从CDI和其他javaEE 6功能中受益,而不必维护该库的两个版本(java EE和java SE),我想对控制台应用程序使用openejb(嵌入式)。 So I've built a maven project in eclipse and added the openejb artifact. 因此,我在Eclipse中构建了一个Maven项目,并添加了openejb工件。

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. 我只是不知道如何使控制台程序使用openejb-container,即解决了我的注入和其他javaEE功能。

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? 因此,我如何使用main()方法获得一个普通的旧Java类,使它使用嵌入式openejb实例化A的成员? - 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. 最后,我的目的是为内部使用嵌入式javaEE容器的库提供Java SE api。

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). 继续并使用EJB对库的行为进行建模(如代码示例所示)。 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. 然后,您的Web应用程序(假设它在同一容器中)可以直接使用这些EJB。

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. 为了通过控制台应用程序访问它,您可以在应用程序客户端容器中运行它(比尝试将容器嵌入应用程序中要好),或者(我建议)以其他方式公开您的业务逻辑(例如通过REST),并在独立的客户端应用程序中使用它。

PS: For integration testing your business logic with DI mechanisms, use Arquillian . PS:要使用DI机制集成测试您的业务逻辑,请使用Arquillian

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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