0

场景:1)创建maven ear项目,在里面创建war项目2)将beans.xml添加到war项目的WEB-INF/ 3)创建简单的@Steateless bean

项目 - http://drp.ly/1j5C3t

ejb 豆:

@Stateless
@LocalBean
public class TestEjb {


    @Inject
    Logger log;

    public TestEjb() {
    }

    @Schedule(hour = "*", minute = "*", second = "*/15")
    public void print1Partner() {
        log.info("Yrjaaaa");
        System.out.println("This is test");

    }


}

战争项目的maven pom-xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Testo</artifactId>
        <groupId>ua.co.testo</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Testo-web</artifactId>

    <packaging>war</packaging>

     <properties>
        <project.build.sourceEncoding>
            UTF-8
        </project.build.sourceEncoding>

        <project.reporting.outputEncoding>
            UTF-8
        </project.reporting.outputEncoding>        
    </properties>



    <dependencies>

        <dependency>
            <groupId>ua.eset.oasys</groupId>
            <artifactId>utopia</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>ua.eset.oasys</groupId>
            <artifactId>fenix</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- SL4J API -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- SLF4J JDK14 Binding  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Injectable Weld-Logger -->
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-logger</artifactId>
            <version>1.0.0-CR2</version>
            <scope>provided</scope>
        </dependency>
            <!--CDI-->
       <!-- <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
            <version>1.0-CR4</version>
        </dependency>-->



        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0.2-FCS</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.2-FCS</version>
            <scope>provided</scope>
        </dependency>


        <!--J2EE-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.ejb</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>



        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>3.0</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <build>
        <finalName>testo-web</finalName>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <encoding>utf8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

ear项目的maven pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Testo</artifactId>
        <groupId>ua.co.testo</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Testo-ear</artifactId>
    <packaging>ear</packaging>

    <properties>
        <project.build.sourceEncoding>
            UTF-8
        </project.build.sourceEncoding>

        <project.reporting.outputEncoding>
            UTF-8
        </project.reporting.outputEncoding>

    </properties>

    <dependencies>



        <!--weld-->
        <!-- SL4J API -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- SLF4J JDK14 Binding  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- Injectable Weld-Logger -->
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-logger</artifactId>
            <version>1.0.0-CR2</version>
        </dependency>
<!--
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>

            <version>1.0-CR4</version>
        </dependency>-->

        <dependency>
            <groupId>ua.co.testo</groupId>
            <artifactId>Testo-web</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>


    </dependencies>

    <build>
        <finalName>testo</finalName>
        <plugins>
            <plugin>
                <!--<groupId>org.apache.maven.plugins</groupId>-->
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <modules>

                        <webModule>
                            <bundleFileName>testo-web.war</bundleFileName>
                            <groupId>ua.co.testo</groupId>
                            <artifactId>Testo-web</artifactId>
                        </webModule>



                        <!--weld-->
                        <!-- SL4J API -->
                        <jarModule>
                            <groupId>org.slf4j</groupId>
                            <artifactId>slf4j-api</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>

                        <!-- SLF4J JDK14 Binding  -->
                        <jarModule>
                            <groupId>org.slf4j</groupId>
                            <artifactId>slf4j-jdk14</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>

                        <!-- Injectable Weld-Logger -->
                        <jarModule>
                            <groupId>org.jboss.weld</groupId>
                            <artifactId>weld-logger</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>
                        <!--<jarModule>
                            <groupId>javax.enterprise</groupId>
                            <artifactId>cdi-api</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>-->

                    </modules>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

测试用例 1(问题):使用 beans.xml 部署项目给出异常:

[#|2010-07-04T19:36:50.003+0300|警告|oracle-glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=33;_ThreadName=Thread-1 ;|在调用 EJB TestEjb 方法时发生系统异常 public void TestEjb.print1Partner() javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: 无法在 com.sun.ejb 创建无状态 EJB。 container.StatelessSessionContainer._getContext(StatelessSessionContainer.java:448) 在 com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2467) 在 com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1860)在 com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1667) 在 com.sun.ejb.containers 的 com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:3962)。EJBTimerService.access$100(EJBTimerService.java:98) at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:2485) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441 ) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask( ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 原因:javax.ejb.EJBException: javax .ejb.CreateException:无法在 com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:720) 在 com.sun.ejb.containers.util.pool 创建无状态 EJB。NonBlockingPool.getObject(NonBlockingPool.java:200) at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:443) ... 12 更多原因:javax.ejb.CreateException:无法在 com 创建无状态 EJB .sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:528) 在 com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:90) 在 com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory。 create(StatelessSessionContainer.java:718) ... 14 更多原因: org.jboss.weld.manager.BeanManagerImpl.getBean 的 java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:768) 的 java.lang.NullPointerException (BeanManagerImpl.java:1171) 在 org.jboss.weld.manager.BeanManagerImpl。getBean(BeanManagerImpl.java:132) 在 org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:145) 在 org.glassfish.weld.services.JCDIServiceImpl.createJCDIInjectionContext(JCDIServiceImpl.java:122) 在 com.sun .ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1616) 在 com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:469) ... 16 更多 |#] [#|2010-07-04T19 :36:50.004+0300|INFO|oracle-glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=33;_ThreadName=Thread-1;|EJB5119:清除计时器 [' 13@@1278255602087@@server@@oasys''TimedObject = TestEjb''应用程序 = testo''BEING_DELIVERED''PERIODIC''容器 ID = 83771744647118848' 'Sun Jul 04 19:36:45 EEST 2010' '0' '*/15 # * # * # * # * # * # * # null # null # null # true # print1Partner # 0' ] [2] 交付失败后|#]

[#|2010-07-04T19:37:00.000+0300|INFO|oracle-glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=33;_ThreadName=Thread -1;|这是测试|#]

期待结果:

伊尔贾阿

这是测试

结果 :

例外

这是测试


案例 2:删除 beans.xml

期待结果:

伊尔贾阿

这是测试

结果

伊尔贾阿

这是测试

目标:我想在包含其他战争和 ejb 项目的耳朵档案内的 web 项目中使用 ejb 焊接(CDI),但我不希望分离我的 web 项目(战争 + ejb)

工具和环境:maven & ejb 3.1 &weld & glassfish 3.0.1

Q1:这可能是某种特定的玻璃鱼虫吗?Q2:什么可能是问题的原因?Q3:这种情况有什么设计吗?

谢谢你。

4

2 回答 2

3

要么将您的 EJB 打包在.war 中,要么将其作为ejb-jar 打包“传统” .ear中。我认为.war打包对于没有强烈模块化需求的项目来说是一种便利。但这并不意味着使用.ear包装时规则发生了变化。

换句话说,我认为您不能混合使用这两种方法,也不希望容器支持它。

作为旁注,我想知道您为什么要cdi-api在 EAR 中打包,我认为 API 是由容器提供的。

在我看来,你应该尽量保持简单。

于 2010-07-04T17:54:18.290 回答
1

看起来我们t use Injection of weld logger in ejb in a case of glassfish ejb container because of it is ejb container managed bean and it don看不到制片人

当前的设计是分离 ejb 和 war 模块 - 在列表中它为我分叉。

于 2010-07-30T10:02:39.583 回答