User: pra
Date: 01/08/07 14:09:00
Added: src/examples/org/jboss/docs/jms/ra/bean TopicHelloBean.java
TopicHelloBean22.java
Log:
Added example code for new JMS chapter
Revision Changes Path
1.1
manual/src/examples/org/jboss/docs/jms/ra/bean/TopicHelloBean.java
Index: TopicHelloBean.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.docs.jms.ra.bean;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import org.jboss.docs.jms.ra.interfaces.*;
/**
* Hello bean, send a message to the configured topic. The JMS stuff is configured
via the deployment descriptor.
*
* The TopicConnection is comparable to the JDBC DataSource, and the TopicSession to
the jdbc Connection.
*
*
* @author Peter Antman
* @version $Revision: 1.1 $
*/
public class TopicHelloBean implements SessionBean {
/**
* Name used to lookup TopicConnectionFactory
*/
private static final String CONNECTION_JNDI =
"java:comp/env/jms/MyTopicConnection";
/**
* Name used to lookup topic destination
*/
private static final String TOPIC_JNDI = "java:comp/env/jms/TopicName";
private SessionContext ctx = null;
private Topic topic = null;
private TopicConnection topicConnection = null;
public TopicHelloBean() {
}
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
public void ejbCreate() {
try {
Context context = new InitialContext();
// Lookup the topic
topic = (Topic)context.lookup(TOPIC_JNDI);
// Lookup the connection factory
TopicConnectionFactory factory =
(TopicConnectionFactory)context.lookup(CONNECTION_JNDI);
topicConnection = factory.createTopicConnection();
// Keep both around
} catch (Exception ex) {
// JMSException or NamingException could be thrown
ex.printStackTrace();
throw new EJBException(ex.toString());
}
}
/**
* Send a message with a message nr in property MESSAGE_NR
*/
public void hello(String msg) {
sendMessage(msg);
}
public void ejbRemove() throws RemoteException {
if(topicConnection != null) {
try {
// Rememer to close the connection when bean is destroyed
topicConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void ejbActivate() {}
public void ejbPassivate() {}
/**
* Help method to send message via JMS
*/
private void sendMessage(String msg) {
TopicSession topicSession = null;
try {
TopicPublisher topicPublisher = null;
TextMessage message = null;
// Create a session
topicSession =
topicConnection.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
// Create a publisher
topicPublisher = topicSession.createPublisher(topic);
// Create a message
message = topicSession.createTextMessage();
message.setText(msg);
// Publish it
System.out.println("Publishing message " + msg);
topicPublisher.publish(message);
} catch (JMSException ex) {
ex.printStackTrace();
ctx.setRollbackOnly();
throw new EJBException(ex.toString());
} finally {
// ALWAYS close the session. It's pooled, so do not worry.
if (topicSession != null) {
try {
topicSession.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
1.1
manual/src/examples/org/jboss/docs/jms/ra/bean/TopicHelloBean22.java
Index: TopicHelloBean22.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.docs.jms.ra.bean;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import org.jboss.docs.jms.ra.interfaces.*;
/**
* Hello bean, send a message to the configured topic. The JMS stuff is configured
via the deployment descriptor.
*
* Works with 2.2.x due to non transacted session
*
* The TopicConnection is comparable to the JDBC DataSource, and the TopicSession to
the jdbc Connection.
*
*
* @author Peter Antman
* @version $Revision: 1.1 $
*/
public class TopicHelloBean22 implements SessionBean {
/**
* Name used to lookup TopicConnectionFactory
*/
private static final String CONNECTION_JNDI =
"java:comp/env/jms/MyTopicConnection";
/**
* Name used to lookup topic destination
*/
private static final String TOPIC_JNDI = "java:comp/env/jms/TopicName";
private SessionContext ctx = null;
private Topic topic = null;
private TopicConnection topicConnection = null;
public TopicHelloBean22() {
}
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
public void ejbCreate() {
try {
Context context = new InitialContext();
// Lookup the topic
topic = (Topic)context.lookup(TOPIC_JNDI);
// Lookup the connection factory
TopicConnectionFactory factory =
(TopicConnectionFactory)context.lookup(CONNECTION_JNDI);
topicConnection = factory.createTopicConnection();
// Keep both around
} catch (Exception ex) {
// JMSException or NamingException could be thrown
ex.printStackTrace();
throw new EJBException(ex.toString());
}
}
/**
* Send a message with a message nr in property MESSAGE_NR
*/
public void hello(String msg) {
sendMessage(msg);
}
public void ejbRemove() throws RemoteException {
if(topicConnection != null) {
try {
// Rememer to close the connection when bean is destroyed
topicConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void ejbActivate() {}
public void ejbPassivate() {}
/**
* Help method to send message via JMS
*/
private void sendMessage(String msg) {
TopicSession topicSession = null;
try {
TopicPublisher topicPublisher = null;
TextMessage message = null;
// Create a session
// To work with 2.2.x we have to make it either not transacted
// or write JMS transaction code
topicSession =
topicConnection.createTopicSession(
// NOT transacted
false,
Session.AUTO_ACKNOWLEDGE);
// Create a publisher
topicPublisher = topicSession.createPublisher(topic);
// Create a message
message = topicSession.createTextMessage();
message.setText(msg);
// Publish it
System.out.println("Publishing message " + msg);
topicPublisher.publish(message);
} catch (JMSException ex) {
ex.printStackTrace();
ctx.setRollbackOnly();
throw new EJBException(ex.toString());
} finally {
// ALWAYS close the session. It's pooled, so do not worry.
if (topicSession != null) {
try {
topicSession.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development