|
PostToNewsService.java - Web Service Client
ListenToNewsService.java - Web Service Client
package oracle.otnsamples.jmswebservice.ejb;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class NewsQueueEJB implements MessageDrivenBean, MessageListener {
private MessageDrivenContext context;
private TopicConnectionFactory topicConnectionFactory = null;
private DataSource ds = null;
public void ejbCreate() throws RemoteException {
try {
Context context = new InitialContext( );
ds = (DataSource) context.lookup("jdbc/NewsDS");
} catch(NamingException nameEx) {
throw new RemoteException(" Error accessing DataSource : " +
nameEx.toString());
}
}
public void onMessage(Message inMsg) {
ObjectMessage objMsg = null;
String destTopicConnFactoryName = null;
Destination destTopic = null;
Element newsElement = null;
try {
if (inMsg instanceof ObjectMessage) {
objMsg = (ObjectMessage) inMsg;
newsElement = (Element)objMsg.getObject();
System.out.println( "News Service MDB : Received news " );
destTopicConnFactoryName = inMsg.getStringProperty("OC4J_REPLY_TO_FACTORY_NAME");
destTopic = inMsg.getJMSReplyTo();
this.storeNews(getNodeValueByName(newsElement,"NewsType"),
getNodeValueByName(newsElement,"Title"),
getNodeValueByName(newsElement,"Snippet"),
getNodeValueByName(newsElement,"DetailedNews"));
this.publishToTopic(destTopicConnFactoryName,destTopic,newsElement);
}
} catch (SQLException sqlEx) {
System.out.println("Error storing news : " + sqlEx.toString());
} catch (Exception ex) { System.out.println(" Error processing news message : "+ex.toString());
}
}
public void initTopicConnectionFactory(String topicConnFactoryName)
throws NamingException {
if(topicConnectionFactory == null) {
Context context = new InitialContext( );
topicConnectionFactory = (TopicConnectionFactory)
context.lookup("java:comp/env/"+topicConnFactoryName);
}
}
private void publishToTopic(String destTopicConnFactoryName,Destination destTopic,
Element newsElement)
throws JMSException {
TopicConnection topicConnection = null;
TopicSession topicSession = null;
Topic topic = null;
TopicPublisher topicPublisher = null;
ObjectMessage outMsg = null;
try {
initTopicConnectionFactory(destTopicConnFactoryName);
topic = (Topic)destTopic;
topicConnection = topicConnectionFactory.createTopicConnection();
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicPublisher = topicSession.createPublisher(topic);
topicConnection.start();
outMsg = topicSession.createObjectMessage();
outMsg.setObject ((Serializable)newsElement);
topicPublisher.publish(outMsg);
System.out.println("News Service: News dispatched successfully");
} catch (NamingException nameEx) {
System.out.println("Error accessing topic : " + nameEx.toString());
} catch (JMSException jmsEx) {
System.out.println("Error publishing to topic : " + jmsEx.toString());
} finally {
if (topicConnection != null) topicConnection.close();
}
}
private void storeNews(String type, String title, String snippet, String detailedNews )
throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
String insertsql = " insert into News "+
" values( seqNewsId.nextval, sysdate,?,?,?,? ) ";
try {
conn = ds.getConnection();
pstmt = conn.prepareStatement( insertsql );
pstmt.setString(1, type);
pstmt.setString(2, title);
pstmt.setString(3, snippet);
pstmt.setString(4, detailedNews);
pstmt.executeUpdate();
} finally {
if(pstmt != null) pstmt.close();
if(conn!=null) conn.close();
}
}
private String getNodeValueByName(Node node, String name) {
String value = null;
try {
if (node != null) {
NodeList children = node.getChildNodes();
int childLen = children.getLength();
for (int ctr=0; ctr < childLen; ctr++) {
Node child = children.item(ctr);
if ((child != null) && (child.getNodeName() != null) &&
child.getNodeName().equals(name) ) {
Node grandChild = child.getFirstChild();
if (grandChild.getNodeValue() != null)
return grandChild.getNodeValue();
}
}
}
} catch(NullPointerException ne) {
value = null;
}
return value;
}
public void ejbRemove() { }
public void setMessageDrivenContext(MessageDrivenContext ctx) {
this.context = ctx;
}
}
|