1.Generally speaking the only way to get a Container-Managed Resource is via dependency injection or lookup from within a Container-Managed Component.
2. Singleton
a.Singletons have an '@Startup' annotation which can be applied to the bean class. When used, the Container will instantiate the Singleton instance eagerly when the application start up, otherwise the Container will instantiate the Singleton instance lazily when the bean is first accessed.
@DependsOn({"SingletonB", "SingletonC"})
@Singleton
@Startup
public class SingletonA {
...
}
b.Note that @DependsOn is only required(and should only be used) if a Singleton uses another Singleton in its @PostConstruct method or @PreDestory method. Simply having a reference to another Singleton and using it in other business methods does not require an @DependsOn declaration.
3. Containers
a.CMP_ENTITY
1.openejb.xml
<Container id="Foo" type="CMP_ENTITY">
</Container>
2.properties file
Foo=new://Container?type=CMP_ENTITY
3.property
CmpEngineFactory - org.apache.openejb.core.cmp.jpa.JpaCmpEngineFactory
b.BMP_ENTITY
1.openejb.xml
<Container id="Foo" type="BMP_ENTITY">
</Container>
2.properties file
Foo=new://Container?type=BMP_ENTITY
3.property
PoolSize - 10
c.STATELESS
1.openejb.xml
<Container id="Foo" type="STATELESS">
</Container>
2.properties file
Foo=new://Container?type=STATELESS
3.property
Timeout - 0 millisecond
PoolSize - 10
StrictPooling - true
d.STATEFUL
1.openejb.xml
<Container id="Foo" type="STATELESS">
</Container>
2.properties file
Foo=new://Container?type=STATEFUL
3.property
Passivator - org.apache.openejb.core.stateful.RAFPassivater
- org.apache.openejb.core.stateful.Simplepassivater(default)
TimeOut - 20 minutes
PoolSize - 1000
BulkPassivate - 100
e.MESSAGE
1.openejb.xml
<Container id="Foo" type="MESSAGE">
</Container>
2.properties file
Foo=new://Container?type=MESSAGE
3.property
ResourceAdapter - The resource adapter delivers messages to the container. Default value is Default JMS Resource Adapter
MessageListenerInterface - Specifies the message listener interface handled by this container. Default value is javax.jms.MessageListener.
ActivationSepcClass - Specifies the activation spec class. Default value is org.apache.activemq.ra.ActionMQActivationSpec.
instanceLimit - Specifies the maximum number of bean instances that are allowed to exists for each MDB deployment. Default value is 10.
f.Resource
1.javax.sql.DataSource
<Resource id="Foo" type="javax.sql.DataSource">
</Resource>
Foo=new://Resource?type=javax.sql.DataSource
properties:JtaManaged, JdbcDriver, JdbcUrl, UserName, Password, ConnectionProperties, DefaultAutoCommit, DefaultReadOnly, DefaulTransactionIsolation, InitialSize, MaxActive, maxIdle, MinIdle, MaxWait, ValidationQuery, TestOnBorrow, TestOnReturn, TestWhileIdle, TimeBetweenEvictionRunsMillis, NumTestsPerEvictionRun, MinEvictableIdleTimeMillis, PoolPreparedStatements, MaxOpenPreparedStatements, AccessToUnderlyingConnectionAllowed
2.ActiveMQResourceAdapter
<Resource id="Foo" type="ActiveMQResourceAdapter">
</Resource>
Foo=new://Resource?type=ActiveMQResourceAdapter
properties:
BrokerXmlConfig - broker:(tcp://localhost:61616)?useJmx=false
ServerUrl - vm://localhost?async=true
DataSource - Default value is Default Unmanaged JDBC Database
3.javax.jms.ConnectionFactory
<Resource id="Foo" type="javax.jms.ConnectionFactory">
</Resource>
Foo = new://Resource?type=javax.jms.ConnectionFactory
properties:
ResourceAdapter
TransactionSupport
PoolMaxSize
PoolMinSize
ConnectionMaxWaitMilliseconds
ConnectionMaxIdleMinutes
4.javax.jms.Queue
<Resource id="Foo" type="javax.jms.Queue">
</Resource>
Foo = new://Resource?type=javax.jms.Queue
properties:
destination - Specifies the name of the queue
5.javax.jms.Topic
<Resource id="Foo" type="javax.jms.Topic">
</Resource>
Foo = new://Resource?type=javax.jms.Topic
properties:
destination - Specifies the name of the topic
6.org.omg.CORBA.ORB
<Resource id="Foo" type="org.omg.CORBA.ORB">
</Resource>
Foo = new://Resource?type=org.omg.CORBA.ORB
7.javax.mail.Session
<Resource id="Foo" type="javax.mail.Session">
</Resource>
Foo = new://Resource?type=javax.mail.Session
4. Configuring Datasource
@Resource(name = "myDerbyDatasource", type = javax.sql.DataSource.class)
<Resource id="myDerbyDatasource" type="javax.sql.DataSource">
. . . .
<Resource>
5. JNDI names for configured DataSources
<Resource id="Default JDBC Database" type="DataSource">
. . . . .
</Resource>
The global jndi name would be java:openejb/Resource/Default JDBC Database
6. Obtaining a DataSource
DataSource references in your EJB should get automatically mapped to the Resource you declare. The shortest and easiest rule is that 'if your reference name matches a Resource in you openejb.xml, that's the one you get'. Essentially, the rules for mapping are as follows:
1. Name Attribute Match - @Resource with a name attribute matching the resource name gets that resource injected
2. Injected Name Match - variable name matching the resource name gets that resource injected
3. No Match - nothing matches a resource name, so the first resource available gets injected
e.g.:
Resource:
<Resource id="myDerbyDatabase" type="DataSource">
. . . . .
</Resource>
a. BY matching variable name to resource name:
@Stateless
public class FooBean {
@Resource DataSource myDerbyDatabase;
}
b. BY matching name
@Stateless
public class FooBean {
@Resource(name="myDerbyDatabase")
DataSource dataSource;
}
c. BY JNDI lookup
@Resource(name="myDerbyDatabase", type=javax.sql.DataSource.class)
@Stateless
public class FooBean {
public void setSessionContext(SessionContext sessionContext) {
DataSource dataSource = (DataSource) sessionContext.lookup("myDerbyDatabase");
}
public void someOtherMethod() throws Exception {
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/myDerbyDatabase");
}
}
<resource-ref>
<res-ref-name>myDerbyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
<resource-ref>
<res-ref-name>jdbc/myDerbyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
<resource-ref>
<res-ref-name>someOtherName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<mapped-name>myDerbyDatabase</mapped-name>
</resource-ref>
7. Working with OpenJPA
a.jndi.properties
java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory
java.naming.provider.url=ejbd://localhost:4201
oracleDatabase=new://Resource?type=DataSource
oracleDatabase.JdbcDriver=oracle.jdbc.driver.OracleDriver
oracleDatabase.JdbcUrl=jdbc:oracle:thin:@10.199.130.142:1521:apj
oracleDatabase.UserName=trs
oracleDatabase.Password=trs
b.persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="oracle">
<jta-data-source>oracleDatabase</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" /> <!-- auto_ddl=true -->
<property name="openjpa.Log" value="SQL=TRACE" /> <!-- print SQL -->
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=80" /> <!-- pretty print SQL -->
</properties>
</persistence-unit>
</persistence>
c.ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<display-name>ejb3</display-name>
</ejb-jar>
d.PersistenceContext injection
@PersistenceContext(unitName = "oracle")
protected EntityManager entityManager;
e.EJBTest.java
import java.io.IOException;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.junit.Before;
public abstract class EJBTest {
private InitialContext ctx;
public EJBTest() {
}
@Before
public void initialise() {
final Properties props = new Properties();
try {
props.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("ejb-test-configure.properties"));
this.ctx = new InitialContext(props);
this.init();
} catch (NamingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected <T> T getEJBBean(final Class<T> clz) {
final String jndiName = clz.getSimpleName() + "ImplRemote";
return this.getEJBBean(jndiName);
}
@SuppressWarnings("unchecked")
protected <T> T getEJBBean(final String jndiName) {
T bean = null;
try {
bean = (T) this.ctx.lookup(jndiName);
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
public abstract void init();
}
8. EJB bean lifecycle
a.stateless: new instance() -> dependency inject -> PostConstruct callback -> application invocation -> PreDestory callback;
b.stateful: new instance() -> dependency inject -> PostConstruct callback -> ready status(app invoke) -> PrePassivate(opt.) -> Passive status -> PostActivate(opt.) -> ready status -> PreDestroy
c.entity: new() -> new -> persist() -> managed -> remove() -> removed ->persist() -> managed -> persistence context ends -> detached -> merge() -> managed -> refresh() -> managed.
9. Setting jndi name pattern in OpenEJB
a.put properties - openejb.jndiname.format={interfaceClass} in {OpenEJB_HOME}/conf/system.properties(create it if not existing)
10. Setting Oracle datasource
a.add 'Resource' in openejb.xml;
<Resource id="oracleDS" type="DataSource">
JdbcDriver oracle.jdbc.driver.OracleDriver
JdbcUrl jdbc:oracle:thin:@10.199.130.142:1521:apj
UserName trs
Password trs
JtaManaged true
</Resource>
b.copy the ojdbc14.jar to {OpenEJB_HOME}/lib/;
c.refer to the 'Resource' in persistence.xml;
<persistence-unit name="oracle" transaction-type="JTA">
<jta-data-source>oracleDS</jta-data-source>
<properties>
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
11. setting deploy folders / projects:
<Deployments dir="apps/" />
<Deployments dir="c:\my\app\beans\" />
<Deployments jar="c:\my\app\superEjbs.jar" />
发表评论
-
Eclipse
2010-12-22 13:29 6611. highlight warning log mes ... -
Java Advance
2010-12-22 13:27 8241. A class with a given name ... -
Spring
2010-12-22 13:25 7971. @Autowired vs. setter @A ... -
JPA Toplink
2010-12-22 13:25 15411.persistence.xml --------- ... -
Apache Ant
2010-12-22 13:22 7721. Add additional command li ... -
Trouble Shooting
2010-12-22 13:22 17901. Problem: log4j:WARN D ... -
Maven Basic
2010-12-22 13:20 12090. useful commands: mvn ...
相关推荐
《J2EE EJB编程实例解析》 J2EE(Java 2 Platform, Enterprise Edition)是Java平台上用于构建企业级应用的框架,它提供了一系列的服务和组件,包括Servlet、JSP、EJB(Enterprise JavaBeans)等。EJB是J2EE的核心...
EJB(Enterprise JavaBeans)是J2EE的核心组成部分,它是服务器端的组件模型,用于构建可复用的业务逻辑。 EJB技术主要包含三种类型的组件: 1. **会话Bean(Session Beans)**:代表客户端的一次会话,用于处理...
**J2EE EJB 3.1:企业级Java组件的增强与演进** Java 2 Platform, Enterprise Edition (J2EE) 是一个用于构建分布式、多层的企业级应用程序的平台,而Enterprise JavaBeans (EJB) 是J2EE的核心组成部分,它提供了一...
《深入浅出J2EE EJB编程实例》 在Java企业级开发中,EJB(Enterprise JavaBeans)是核心组件之一,它为构建分布式、事务处理、安全的业务应用程序提供了强大的支持。本压缩包“j2ee.ejb编程实例.rar”正是针对这一...
j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee ...
j2ee ejb编程实例,全源码全源码全源码
EJB(Enterprise JavaBeans)是J2EE的核心部分,提供了一种在Java环境中创建可重用的服务器端组件的方式。 J2EE的组件包括: 1. **Java Transaction Service (JTS)**:提供事务管理服务,确保在多操作中的数据一致...
EJB(Enterprise JavaBeans)和RMI(Remote Method Invocation)是Java开发中两个重要的概念,尤其是在J2EE(Java 2 Platform, Enterprise Edition)环境中。这两个技术都是Java平台为企业级应用提供服务的核心组件...
JMS是Java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。JMS支持两种消息模型:Point-to-Point(P2P)和Publish/Subscribe(Pub/Sub),即点对点和发布订阅模型。
j2ee without ejb 中文 part3j2ee without ejb 中文 part3j2ee without ejb 中文 part3j2ee without ejb 中文 part3j2ee without ejb 中文 part3j2ee without ejb 中文 part3j2ee without ejb 中文 part3
目前Tomcat并不直接支持EJB,但可借助openejb插件来间接能够支持EJB
Expert One-on-One J2EE Development Without EJB中文版.pdf.7z.002(共3个)
本教程将深入探讨EJB技术及其在J2EE环境中的应用。 一、EJB概述 EJB是一种组件模型,它定义了在Java EE服务器中运行的业务逻辑组件的接口和实现。这些组件提供了服务,如事务管理、安全性、持久性以及并发控制,...
标题中的“J2EE Jboss Ejb With Eclipse 2003.rar”表明这是一个关于使用Eclipse集成开发环境(IDE)在JBoss应用服务器上开发Java Enterprise Edition (J2EE) 的Enterprise JavaBeans (EJB) 的教程或资料包。...
Expert One-on-One J2EE Development Without EJB中文版.pdf.7z.001 (共3个)