Tento zapisek zcela vychazi z http://weblogs.java.net/blog/rampsarathy/archive/2007/03/glassfish_v2_an.html, kde se snazim celou situaci namapovat na uvedene verze s tim, ze opravim male drobnosti, pripadne uvest nekde doplnujici info. Uvedeny priklad bude v EJB3 (kombinace anotaci a xml descriptoru), prestoze v prvotnim testovani je volani JNDI nejlepsi zpusob debugovani. Cilem je pouzit Active MQ jako JMS providera v AS Glassfish a schopnost poslat/prijmout zpravu pomoci tohoto providera.
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put(„java.naming.factory.initial“, „com.sun.jndi.fscontext.RefFSContextFactory“);
props.put(„java.naming.provider.url“, „file:/home/nemecm/activemqobjects“);
InitialContext jndiContext = new InitialContext(props);
ActiveMQQueue mq = new ActiveMQQueue(„Send“);
ActiveMQQueue outmq = new ActiveMQQueue(„Receive“);ActiveMQTopic top = new ActiveMQTopic(„ReceiveTopic“);
ActiveMQXAConnectionFactory confac = new ActiveMQXAConnectionFactory();
ActiveMQConnectionFactory confac1 = new ActiveMQConnectionFactory();
jndiContext.rebind(„Send“, mq);
jndiContext.rebind(„Receive“, outmq);
jndiContext.rebind(„ReceiveTopic“, top);
jndiContext.rebind(„activemqconnectionfactory“, confac);
jndiContext.rebind(„activemqnonxaconnectionfactory“, confac1);
}catch (Exception e) {
System.out.println("Could not create JNDI " + "context: "+ e.toString());
e.printStackTrace();
System.exit(1);
}
}
Jen upozornim ze knihovnu fscontext.jar je mozno nalezt v ${GLASSFISH_HOME}/imq/lib, dale posledni element v ceste (zde activemqobjects) je adresar a musi existovat pred spustenim programu.
./asadmin create-resource-adapter-config –user admin –passwordfile ~/.aspasswd
–property SupportsXA=true:ProviderIntegrationMode=jndi:RMPolicy=OnePerPhysicalConnection: JndiProperties=java.naming.factory.initial\\=com.sun.jndi.fscontext.RefFSContextFactory, java.naming.provider.url\\=file\\:/home/nemecm/activemqobjects:LogLevel=FINEST genericra
./asadmin deploy –user admin –passwordfile ~/.aspasswd ~/servers/glassfish/lib/addons/resourceadapters/genericjmsra/genericra.rar
./asadmin create-connector-connection-pool –raname genericra –connectiondefinition javax.jms.QueueConnectionFactory –transactionsupport XATransaction –property ConnectionFactoryJndiName=activemqconnectionfactory inpool
./asadmin create-connector-connection-pool –raname genericra –connectiondefinition javax.jms.QueueConnectionFactory –transactionsupport XATransaction –property ConnectionFactoryJndiName=activemqconnectionfactory outpool
./asadmin create-connector-resource –poolname inpool jms/inboundXAQCF
./asadmin create-connector-resource –poolname outpool jms/outboundXAQCF
./asadmin create-admin-object –raname genericra –restype javax.jms.Queue –property DestinationJndiName=Receive jms/inqueue
./asadmin create-admin-object –raname genericra –restype javax.jms.Queue –property DestinationJndiName=Send jms/outqueue
Session's XAResource has not been enlisted in a distributed transaction.
Bussines interface
public interface ExchangeRateProducer {
void sendExchangeRate(String xml);
}
a jeho implementace
public class ExchangeRateProducerBean implements ExchangeRateProducer {
private static Log log = LogFactory.getLog(ExchangeRateProducerBean.class);
@Resource(name=„connectionFactory“)
private ConnectionFactory connectionFactory;
@Resource(name=„queue“)
private Queue queue;
@Resource
private SessionContext context;public void sendExchangeRate(String xml) {
UserTransaction transaction = context.getUserTransaction();
try {
try {
transaction.begin();
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage msg = session.createTextMessage();
msg.setText(xml);
producer.send(msg);
session.close();
connection.close();
transaction.commit();
} catch (JMSException e) {
log.error(„JMSException catched.“, e);
transaction.rollback();
}
} catch (Exception e) {
log.error(„Exception occured.“, e);
}
}
}
}
Male intermezzo: this blog engine SUCKS! Nenasel jsem jednoduchy zpusob (popravde nemam moc chut neco zkoumat) jak vlozit xml text, zalamovani je tragicke. Asi to neni urceno k zamerum jake jsem jsou mnou ocekavany. Asi budu muset postovat podobne zapisky jinam ;-(
Urcite jsou potreba ejb-jar.xml a sub-ejb-jar.xml, ale zvyse uvedenych duvodu nemam silu to ted resit. Takze ne zcela spravne nejprve ejb-jar.xml
[enterprise-beans]
[session]
[ejb-name]ExchangeRateProducer[/ejb-name]
[business-remote]com.ness.csa.vhoz.test.services.ExchangeRateProducer[/business-remote]
[ejb-class]com.ness.csa.vhoz.test.services.ExchangeRateProducerBean[/ejb-class]
[session-type]Stateless[/session-type]
[transaction-type]Bean[/transaction-type]
[resource-ref]
[res-ref-name]connectionFactory[/res-ref-name]
[res-auth]Container[/res-auth]
[mapped-name]jms/outboundXAQCF[/mapped-name]
[/resource-ref]
[resource-ref]
[res-ref-name]queue[/res-ref-name]
[res-type]javax.jms.Queue[/res-type]
[mapped-name]jms/outqueue[/mapped-name]
[/resource-ref]
[/session]
[/enterprise-beans]
a sun-ejb-jar.xml
[sun-ejb-jar]
[enterprise-beans]
[ejb]
[ejb-name]ExchangeRateProducer[/ejb-name]
[jndi-name]ejb/ExchangeRateProducer[/jndi-name]
[/ejb]
[/enterprise-beans]
[/sun-ejb-jar]
Po zavolani metody sendExchangeRate(„ahoj“) je mozno prohlednout ziskat tento vysledek v browseru
Tak to je snad vse, mozna nejsou dobre mapovany fronty Receive a Send v obrazich a v kodu, ale to je jen vysledek ruznych testovani ;-).
Bardolf