Wednesday, 20 March 2013

MDB (Message Driven Bean) for news agency

Write a MDB (Message Driven Bean) for news agency that has to
capture the data from various news sources. The newly written MDB
should accept the XML format of the news. The XML data needs to be
parsed and stored in the database. The news format is as follows:
 
<news_id> </ news_id>
<sources> </source>
<date> </date>
<type_of_news> </type_of_news>
<priority> <priority>
<news_content> </news_content>


import java.util.Date;
import java.util.DateFormat;
import java.util.SimpleDateFormat;
import javax.jms.*;
import org.w3c.dom.Document;
import java.sql.*;

private class News{
    private int id;
    public int priority;
    public date;
    public String sources="",type_of_news,news_content;   
    public News(int id){
        this.id=id;
    }
    public int getID(){
        return id;
    }
}


Public class NewsMDB implements MessageDrivenBean,MessageListener {

    private NewsLocal news;
    private Document dom;

    public void setMessageDrivenContext(MessageDrivenContext mdc) {
        try {
            InitialContext ictx = new InitialContext();
        } catch ( Exception ex ) {
            throw new EJBException("Unable to get News Agency bean", ex);
        }
    }
    public void ejbCreate() { }
    public void ejbRemove() { }
    public void onMessage(Message msg) {
        XMLMessage xml = (XMLMessage)msg;
        try {
            dom=xml.getDocument();
            saveInDB(parseDocument());
        } catch ( Exception ex ) {
            throw new EJBException(ex);
        }
    }
    private void saveInDB(News ns){
        try{
            Class.forName("");//Class required for DataBase
            DriverManager.getConnection("","",""); //Database file path & Userid/password
            PreparedStatement st;
            st=con.createPreparedStatement("Insert into news values(?,?,?,?,?,?)");
            st.setInt(1,ns.getID());
            st.setString(2,ns.sources);
            st.setDate(3,ns.date);
            st.setString(4,ns.type_of_news);
            st.setInt(5,ns.priority);
            st.setString(6,ns.news_content);
            st.execute();
            st.close();
            st=null;
            con.close();
            con=null;
        }catch(SQLException se){
            throw new EJBException("Unable to Save News Agency bean", ex);
        }
    }
    private News parseDocument(){
        Element docEle;
        News news;
        //get the root element
        docEle = dom.getDocumentElement();
        //get a nodelist of elements
        int id=getIntValue(docEle,"news_id");
        news=new News(id);
        news.sources=getIntValue(docEle,"sources");
        news.date=getDateValue(docEle,"date");
        news.type_of_news=getStringValue(docEle,"type_of_news");
        news.priority=getIntValue(docEle,"priority");
        news.news_content=getStringValue(docEle,"news_content");
        return news;
    }
   
    private String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if(nl != null && nl.getLength() > 0) {
            Element el = (Element)nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }
        return textVal;
    }
    private int getIntValue(Element ele, String tagName) {       
        return Integer.parseInt(getTextValue(ele,tagName));
    }
    private Date getDateValue(Element ele, String tagName) {
        String startDateString = getTextValue(ele,tagName);
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        Date newDateString;
        try {
                startDate = df.parse(startDateString);
                newDateString = df.format(startDate);
            } catch (Exception e) {
            throw new EJBException(e);
           }
        return newDateString;
    }
}



No comments:

Post a Comment