方案: 1)创建maven ear项目,在里面创建war项目 2)将beans.xml添加到war项目的WEB-INF / 3)创建简单的@Steateless bean
项目 - http://drp.ly/1j5C3t
ejb bean:
@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>
耳朵项目的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 |警告|甲骨文glassfish3.0.1 | javax.enterprise.system.container.ejb.com.sun.ejb.containers | _ThreadID = 33; _ThreadName =线程1; | A 期间发生系统异常 EJB TestEjb方法的调用 public void TestEjb.print1Partner() javax.ejb.EJBException异常: javax.ejb.EJBException异常: javax.ejb.CreateException:不能 创建无状态EJB com.sun.ejb.containers.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.BaseContainer.callEJBTimeout(BaseContainer.java:3962) 在 com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1667) 在 com.sun.ejb.containers.EJBTimerService.access $ 100(EJBTimerService.java:98) 在 com.sun.ejb.containers.EJBTimerService $ TaskExpiredWork.run(EJBTimerService.java:2485) 在 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) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:619) 引起:javax.ejb.EJBException: javax.ejb.CreateException:不能 创建无状态EJB com.sun.ejb.containers.StatelessSessionContainer $ SessionContextFactory.create(StatelessSessionContainer.java:720) 在 com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:200) 在 com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:443) ... 12更多引起: javax.ejb.CreateException:不能 创建无状态EJB com.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更多引起: java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:768) 在 org.jboss.weld.manager.BeanManagerImpl.getBean(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 |甲骨文glassfish3.0.1 | javax.enterprise.system.container.ejb.com.sun.ejb.containers | _ThreadID = 33; _ThreadName =线程1 ; | EJB5119:清除日期 计时器 ['13 @@ 1278255602087 @@服务器@@ OASYS' 'TimedObject = TestEjb''应用= testo''BEING_DELIVERED''PERIODIC' '货柜ID = 83771744647118848' 'Sun Jul 04 19:36:45 EEST 2010''0' '* / 15#*#*#*#*#*#*#null# null #null#true#print1Partner# [2]交付失败后的0'] |#]
[#| 2010-07-04T19:37:00.000 + 0300 | INFO |甲骨文glassfish3.0.1 | javax.enterprise.system.std.com.sun.enterprise.v3.services.impl | _ThreadID = 33; _ThreadName =线程1; |这 是测试|#]
期待结果:
Yrjaaaa
这是测试
结果:
异常
这是测试
案例2: 删除beans.xml
期待结果:
Yrjaaaa
这是测试
结果
Yrjaaaa
这是测试
目标:我想在耳机档案中的网络项目中使用ejb焊接(CDI),其中包括其他一些战争&amp; ejb项目,但我不喜欢将我的网络项目(战争+ ejb)分开
工具&amp;环境:maven&amp; ejb 3.1&amp;焊接和glassfish 3.0.1
Q1:这可能是某种特定的玻璃鱼吗? Q2:可能导致问题的原因是什么? 问题3:这种情况有一些设计吗?
谢谢。
答案 0 :(得分:3)
将您的EJB打包在 .war OR 中将其打包为 ejb-jar 在“传统” .ear <中/ em>的。我认为 .war 包装可以方便没有强大模块化需求的项目。但这并不意味着使用 .ear 包装时规则会发生变化。
换句话说,我不认为你可以混合两种方法,也不希望容器支持它。
作为旁注,我想知道你为什么要在EAR中打包cdi-api
,我认为API是由容器提供的。
在我看来,你应该尽量保持简单。
答案 1 :(得分:0)
看起来我们可以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模块分开 - 在列表中它为我分叉。