<?xml version="1.0" encoding="UTF-8"?>
<openejb>
<Container id="My Singleton Container" type="SINGLETON">
# Specifies the maximum time an invocation could wait for the
# singleton bean instance to become available before giving up.
#
# After the timeout is reached a
javax.ejb.ConcurrentAccessTimeoutException
# will be thrown.
#
# Usable time units: nanoseconds, microsecons, milliseconds,
# seconds, minutes, hours, days. Or any combination such as
# "1 hour and 27 minutes and 10 seconds"
AccessTimeout = 30 seconds
</Container>
<Container id="My Stateful Container" type="STATEFUL">
# Specifies the maximum time an invocation could wait for the
# stateful bean instance to become available before giving up.
#
# After the timeout is reached a
javax.ejb.ConcurrentAccessTimeoutException
# will be thrown.
#
# Usable time units: nanoseconds, microsecons, milliseconds,
# seconds, minutes, hours, days. Or any combination such as
# "1 hour and 27 minutes and 10 seconds"
AccessTimeout = 30 seconds
# The passivator is responsible for writing beans to disk
# at passivation time. Different passivators can be used
# by setting this property to the fully qualified class name
# of the PassivationStrategy implementation. The passivator
# is not responsible for invoking any callbacks or other
# processing, its only responsibly is to write the bean state
# to disk.
#
# Known implementations:
# org.apache.openejb.core.stateful.RAFPassivater
# org.apache.openejb.core.stateful.SimplePassivater
Passivator org.apache.openejb.core.stateful.SimplePassivater
# Specifies the time to wait between invocations. This
# value is measured in minutes. A value of 5 would
# result in a time-out of 5 minutes between invocations.
# A value of zero would mean no timeout.
TimeOut 20
# Specifies the frequency (in seconds) at which the bean cache is checked
for
# idle beans.
Frequency 60
# Specifies the size of the bean pools for this
# stateful SessionBean container.
Capacity 1000
# Property name that specifies the number of instances
# to passivate at one time when doing bulk passivation.
# Must be less than the PoolSize.
BulkPassivate 100
</Container>
<Container id="My Stateless Container" type="STATELESS">
# Specifies the time an invokation should wait for an instance
# of the pool to become available.
#
# After the timeout is reached, if an instance in the pool cannot
# be obtained, the method invocation will fail.
#
# Usable time units: nanoseconds, microsecons, milliseconds,
# seconds, minutes, hours, days. Or any combination such as
# "1 hour and 27 minutes and 10 seconds"
AccessTimeout = 30 seconds
# Specifies the size of the bean pools for this stateless
# SessionBean container. If StrictPooling is not used, instances
# will still be created beyond this number if there is demand, but
# they will not be returned to the pool and instead will be
# immediately destroyed.
MaxSize = 10
# Specifies the minimum number of bean instances that should be in
# the pool for each bean. Pools are prefilled to the minimum on
# startup. Note this will create start order dependencies between
# other beans that also eagerly start, such as other @Stateless
# beans with a minimum or @Singleton beans using @Startup. The
# @DependsOn annotation can be used to appropriately influence
# start order.
#
# The minimum pool size is rigidly maintained. Instances in the
# minimum side of the pool are not eligible for IdleTimeout or
# GarbageCollection, but are subject to MaxAge and flushing.
#
# If the pool is flushed it is immediately refilled to the minimum
# size with MaxAgeOffset applied. If an instance from the minimum
# side of the pool reaches its MaxAge, it is also immediately
# replaced. Replacement is done in a background queue using the
# number of threads specified by CallbackThreads.
MinSize = 0
# StrictPooling tells the container what to do when the pool
# reaches it's maximum size and there are incoming requests that
# need instances.
#
# With strict pooling, requests will have to wait for instances to
# become available. The pool size will never grow beyond the the
# set MaxSize value. The maximum amount of time a request should
# wait is specified via the AccessTimeout setting.
#
# Without strict pooling, the container will create temporary
# instances to meet demand. The instances will last for just one
# method invocation and then are removed.
#
# Setting StrictPooling to false and MaxSize to 0 will result in
# no pooling. Instead instances will be created on demand and live
# for exactly one method call before being removed.
StrictPooling = true
# Specifies the maximum time that an instance should live before
# it should be retired and removed from use. This will happen
# gracefully. Useful for situations where bean instances are
# designed to hold potentially expensive resources such as memory
# or file handles and need to be periodically cleared out.
#
# Usable time units: nanoseconds, microsecons, milliseconds,
# seconds, minutes, hours, days. Or any combination such as
# "1 hour and 27 minutes and 10 seconds"
MaxAge = 0 hours
# Specifies the maximum time that an instance should be allowed to
# sit idly in the pool without use before it should be retired and
# removed.
#
# Usable time units: nanoseconds, microsecons, milliseconds,
# seconds, minutes, hours, days. Or any combination such as
# "1 hour and 27 minutes and 10 seconds"
IdleTimeout = 0 minutes
</Container>
<Resource id="DB2" type="DataSource">
JdbcDriver =com.ibm.db2.jcc.DB2Driver
XADataSource=com.ibm.db2.jcc.DB2XADataSource
JdbcUrl =jdbc:db2://22.104.102.127:61118/wiki
UserName =wiki
Password =123456
JtaManaged =true
</Resource>
<Resource id="H2" type="DataSource">
JdbcDriver=org.h2.Driver
XADataSource=org.h2.jdbcx.JdbcDataSource
JdbcUrl
=jdbc:h2:tcp://22.104.178.97:8080/mem:memdb;DB_CLOSE_DELAY=-1;MODE=DB2
UserName =abc
Password =123
JtaManaged =true
</Resource>
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
# Broker configuration URI as defined by ActiveMQ
# see
http://activemq.apache.org/broker-configuration-uri.html
#BrokerXmlConfig = broker:(tcp://localhost:61616)
# Broker address
ServerUrl = tcp://localhost:61616
</Resource>
<Resource id="ConnectionFactory" type="javax.jms.ConnectionFactory">
ResourceAdapter = MyJmsResourceAdapter
</Resource>
<Container id="MyJmsMdbContainer" type="MESSAGE">
ResourceAdapter = MyJmsResourceAdapter
</Container>
<Resource id="answerQueue" type="javax.jms.Queue"/>
<Deployments dir="apps/" />
</openejb>
mdb
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.ActivationConfigProperty;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Destination;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.ObjectMessage;
import javax.sql.DataSource;
import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;
import java.sql.*;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import com.ibm.db2.jcc.DB2Driver;
import com.ibm.db2.jcc.DB2XADataSource;
import com.ibm.db2.jcc.DB2Xid;
import org.h2.jdbcx.JdbcDataSource;
import org.h2.jdbcx.JdbcXAConnection;
import org.h2.jdbcx.JdbcConnectionPool;
@TransactionManagement(TransactionManagementType.BEAN)
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination",
propertyValue =
"userQueue")
})
public class ChatBean implements MessageDrivenBean,MessageListener {
@Resource(name="ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(name="answerQueue")
private Queue answerQueue;
MessageDrivenContext messageDC;
UserTransaction ut;
DataSource h2,db2;
public void setMessageDrivenContext(MessageDrivenContext mtc)
throws
EJBException
{
messageDC=mtc;
System.out.println("MessageDrivenContext setted!");
try{
Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.RemoteInitialContextFactory");
prop.setProperty(Context.PROVIDER_URL, "ejbd://127.0.0.1:4201");
InitialContext ctx = new InitialContext(prop);
db2 =(DataSource) ctx.lookup("DB2");
h2 =(DataSource) ctx.lookup("H2");
}catch(Exception e){
e.printStackTrace();
}
}
public void ejbRemove() throws EJBException
{
System.out.println("MDB Delete!");
}
public void onMessage(Message message){
try {
///////////////////////////////////////////////////////
final TextMessage textMessage = (TextMessage) message;
final String question = textMessage.getText();
System.out.println("Received="+question);
//May call another business process start
String retString = businessMethod(question);
//May call another business process end
System.out.println(retString);
javax.jms.Connection connection
=
connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer producer =
session.createProducer(answerQueue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a message
TextMessage messageRes =
session.createTextMessage(retString);
// Tell the producer to send the message
producer.send(messageRes);
// Clean up
if (session != null) session.close();
if (connection != null) connection.close();
System.out.println("MDB
called!!!");
} catch (Exception e) {
System.out.println("error!!!");
try{
ut.rollback();
}catch(Exception e1){
System.out.println("rollback
failed!!!");
}
e.printStackTrace();
}
}
public String businessMethod(String message) throws Exception{
System.out.println("businessMethod!!!");
ut=(UserTransaction) messageDC.getUserTransaction();
ut.begin();
//DataSource h2 =(DataSource)
ctx.lookup("H2");
//XAConnection
xa=h2.getXAConnection();
java.sql.Connection
connh2=h2.getConnection();
StringBuffer sql1 = new
StringBuffer(256);
sql1.append(" update
testh set name ='111' where id =1 ");
PreparedStatement
PreStmt1 =
connh2.prepareStatement(sql1.toString());
System.out.println(sql1);
PreStmt1.executeUpdate();
PreStmt1.close();
connh2.close();
//DataSource db2 =(DataSource)
ctx.lookup("DB2");
//XAConnection
conndb2x=db2.getXAConnection();
java.sql.Connection
conndb2=db2.getConnection();
StringBuffer sql = new
StringBuffer(256);
sql.append(" update
TEST27 set name ='131' where id =1 ");
PreparedStatement
PreStmt = conndb2.prepareStatement(sql.toString());
System.out.println(sql);
PreStmt.executeUpdate();
PreStmt.close();
conndb2.close();
ut.commit();
System.out.println("businessMethod
finish!");
return "OK_Earth";
}
}
/////////////////////////////////////////////////////////////////////////////////
mdbclient
//javac -cp
c:\openejb451\lib\javaee-api-6.0-4.jar;c:\openejb451\lib\activemq-core-5.7.0.jar;c:\openejb451\lib\openejb-client-4.5.1.jar;c:\openejb451\lib\db2jcc.jar;c:\
MdbTestClient.java
//java -cp
c:\openejb451\lib\javaee-api-6.0-4.jar;c:\openejb451\lib\activemq-core-5.7.0.jar;c:\openejb451\lib\openejb-client-4.5.1.jar;c:\openejb451\lib\slf4j-api-1.7.2.jar;c:\openejb451\lib\slf4j-jdk14-1.7.2.jar;c:\openejb451\lib\db2jcc.jar;c:\
MdbTestClient
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.ActivationConfigProperty;
import javax.jms.Connection;
import javax.jms.QueueConnection;
import javax.jms.ConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Destination;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.ObjectMessage;
import javax.rmi.PortableRemoteObject;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.sql.DataSource;
public class MdbTestClient {
public static void main(String[]args) throws Exception {
//
System.setProperty("ConnectionFactory","connectionfactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616");
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.RemoteInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL,
"ejbd://127.0.0.1:4201");
//properties.setProperty(Context.SECURITY_PRINCIPAL, "Kevin");
//properties.setProperty(Context.SECURITY_CREDENTIALS, "password");
InitialContext ctx = new InitialContext(properties);
//
ConnectionFactory connectionFactory;
Connection connection = null;
Session session = null;
Queue queue=null,Aqueue=null;
System.out.println("ConnectionFactory="+System.getProperty("ConnectionFactory"));
queue = (Queue) ctx.lookup("userQueue");
Aqueue = (Queue) ctx.lookup("answerQueue");
connectionFactory=(ConnectionFactory)ctx.lookup("java:ConnectionFactory");
//connectionFactory =
(ConnectionFactory)PortableRemoteObject.narrow(ctx.lookup("java:ConnectionFactory"),ConnectionFactory.class);
connection = connectionFactory.createConnection();
connection.start();
// Create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a message
TextMessage message = session.createTextMessage("Hello World!");
// Tell the producer to send the message
producer.send(message);
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(Aqueue);
// Receive response message
final TextMessage
retMessage=(TextMessage)consumer.receive(10000);
final String mess=retMessage.getText();
System.out.println(mess);
// Clean up
if (session != null) session.close();
if (connection != null) connection.close();
}
}
--
View this message in context:
http://openejb.979440.n4.nabble.com/about-openejb-standalone-transaction-control-tp4660358p4660401.html
Sent from the OpenEJB Dev mailing list archive at Nabble.com.