📄 topichellobean.java
字号:
package com.hellking.study.ejb;
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;
/**
* 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: 3.1 $
*/
public class TopicHelloBean implements SessionBean {
/**
* Name used to lookup TopicConnectionFactory
*/
private static final String CONNECTION_JNDI =
"java:comp/env/jms/HellkingTopicConnection";
/**
* Name used to lookup topic destination
*/
private static final String TOPIC_JNDI = "java:comp/env/jms/HellkingTopicName";
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 {
// Remember to close the connection when the 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();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -