package dk.topsecurity;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import javax.ejb.CreateException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.rmi.RemoteException;
public class ServiceBean implements MessageDrivenBean, MessageListener {
private String OUTQUEUE_NAME = "dk.topsecurity.outqueue";
private static final boolean VERBOSE = true;
private MessageDrivenContext m_context;
private Destination destination;
private ConnectionFactory factory;
// You might also consider using WebLogic's log service
private void log(String s) {
if (VERBOSE) System.out.println(s);
}
/**
* These methods are required by the EJB Specification,
* but are not used by this example.
*/
public void ejbActivate() {
log("ejbActivate called -");
}
public void ejbRemove() {
log("ejbRemove called -");
}
public void ejbPassivate() {
log("ejbPassivate called -");
}
/**
* Sets the session context.
* @param ctx MessageDrivenContext Context for session
*/
public void setMessageDrivenContext(MessageDrivenContext ctx) {
log("setMessageDrivenContext called");
m_context = ctx;
}
/**
* This method corresponds to the create method in the home interface.
*/
public void ejbCreate () throws CreateException {
log("ejbCreate called -");
Context context = null;
try {
context = new InitialContext();
} catch (NamingException ne) {
ne.printStackTrace();
throw new CreateException(ne.getMessage());
}
String connectionFactoryName = "weblogic.jms.ConnectionFactory";
try {
factory = (ConnectionFactory)context.lookup(connectionFactoryName);
} catch (Exception e) {
e.printStackTrace();
throw new CreateException(e.getMessage());
}
try {
destination = (Destination)context.lookup(OUTQUEUE_NAME);
} catch (Exception e) {
e.printStackTrace();
throw new CreateException(e.getMessage());
}
}
/**
* Retrieve the value of the Message
*
*/
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage) {
String reply = ((TextMessage) msg).getText();
log("MDB Text: " + reply);
log(reply);
sendText((QueueConnectionFactory)factory, (Queue)destination, reply);
}
else if (msg instanceof ObjectMessage) {
ObjectMessage om = (ObjectMessage) msg;
Object rec = om.getObject();
log("MDB Object: " + rec);
sendObject((QueueConnectionFactory)factory, (Queue)destination,
(Serializable)rec);
}
else {
log("MDB: received an unsupported message type...discarded.");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private static void sendObject(QueueConnectionFactory qfactory, Queue queue,
Serializable o) throws InvocationTargetException
{
QueueConnection qconnection = null;
QueueSession qsession = null;
QueueSender qsender = null;
try {
qconnection = qfactory.createQueueConnection();
qsession = qconnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
qsender = qsession.createSender(queue);
ObjectMessage omessage = qsession.createObjectMessage();
omessage.setObject(o);
qconnection.start();
qsender.send(omessage);
} catch (JMSException jmse) {
throw new InvocationTargetException(jmse, "Could not send message");
} finally {
try {
if (qsender != null) qsender.close();
} catch (JMSException ignore) {}
try {
if (qsession != null) qsession.close();
} catch (JMSException ignore) {}
try {
if (qconnection != null) qconnection.close();
} catch (JMSException ignore) {}
}
}
private static void sendText(QueueConnectionFactory qfactory, Queue queue, String t)
throws InvocationTargetException
{
QueueConnection qconnection = null;
QueueSession qsession = null;
QueueSender qsender = null;
try {
qconnection = qfactory.createQueueConnection();
qsession = qconnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
qsender = qsession.createSender(queue);
TextMessage tmessage = qsession.createTextMessage();
tmessage.setText(t);
qconnection.start();
qsender.send(tmessage);
} catch (JMSException jmse) {
throw new InvocationTargetException(jmse, "Could not send message");
} finally {
try {
if (qsender != null) qsender.close();
} catch (JMSException ignore) {}
try {
if (qsession != null) qsession.close();
} catch (JMSException ignore) {}
try {
if (qconnection != null) qconnection.close();
} catch (JMSException ignore) {}
}
}
}