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 6341. highlight warning log mes ... -
Java Advance
2010-12-22 13:27 7971. A class with a given name ... -
Spring
2010-12-22 13:25 7701. @Autowired vs. setter @A ... -
JPA Toplink
2010-12-22 13:25 15011.persistence.xml --------- ... -
Apache Ant
2010-12-22 13:22 7451. Add additional command li ... -
Trouble Shooting
2010-12-22 13:22 17601. Problem: log4j:WARN D ... -
Maven Basic
2010-12-22 13:20 11760. useful commands: mvn ...
相关推荐
J2EE,EJB,J2EE,EJB,J2EE,EJB,J2EE,EJB,J2EE,EJB,J2EE,EJB,J2EE,EJB,J2EE,EJB,
j2ee ejb编程实例
j2ee ejb开发
j2ee ejb编程实例
j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee without ejb 中文 part2j2ee ...
继ejb3.0后,3.1版本推出的功能更完善
J2EE之EJB,RMI多个源码
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
j2EE_ebj.编程示例, 有源码,需要的速度下载咯 (*^__^*) 嘻嘻
Why J2EE Without EJB.pdf Why J2EE Without EJB.pdf Why J2EE Without EJB.pdf Why J2EE Without EJB.pdf
Java J2EE EJB 最新ejb3.0实例教程
《Expert-One-on-one J2EE Development Without EJB》一书的英文版本
One of the best books from Rod Johnson explaining how to develop J2EE project without EJB.
Expert One-on-One J2EE Development Without EJB中文版.pdf.7z.002(共3个)
J2Ee Jboss Ejb With Eclipse 2003 J2Ee Jboss Ejb With Eclipse 2003
目前Tomcat并不直接支持EJB,但可借助openejb插件来间接能够支持EJB