bai 12- message driven bean - entity beans

Upload: hailuagiao

Post on 05-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    1/12

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    2/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Thm 1 CMP nh sau: Nhn phi chut ln project c hn New->Others, chn nh hnh

    To datasource v chn bng nh sau:

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    3/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    G package name vi tn: vovanhai.wordpress.com.entities

    Chnh sa file LophocLocalHome.java nh sau:

    package vovanhai.wordpress.com.entities;

    import java.util.Collection;import javax.ejb.CreateException;import javax.ejb.EJBLocalHome;import javax.ejb.FinderException;

    /**** @author VoVanHai*/

    public interface LophocLocalHome extends EJBLocalHome {

    vovanhai.wordpress.com.entities.LophocLocal findByPrimaryKey(java.lang.String key) throwsFinderException;

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    4/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    vovanhai.wordpress.com.entities.LophocLocal create(String msLop, String tenLop, StringgiaovienCN) throws CreateException;

    Collection findByMsLop(String msLop) throws FinderException;

    Collection findByTenLop(String tenLop) throws FinderException;

    Collection findByGiaovienCN(String giaovienCN) throws FinderException;

    Collection findAll() throws FinderException;}

    Sa file LophocBean.java nh sau:

    public java.lang.String ejbCreate(String msLop, String tenLop, String giaovienCN) throwsCreateException {

    if (msLop == null) {throw new CreateException("The field \"key\" must not be null");

    }setMsLop(msLop);setTenLop(tenLop);setGiaovienCN(giaovienCN);return null;

    }

    public void ejbPostCreate(String msLop, String tenLop, String giaovienCN) {}

    M file ejb-jar.xmlln, thm 1 nh sau

    findAll

    SELECT OBJECT(l) FROM Lophoc AS l

    Trong jboss.xml, thm phn on khai bo cho entity bean nh sau :

    LophocBeanLophocBean

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    5/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Thm 1 Message-Driven Bean cho vic lng nghe cc Message gi n v slu thng tin xungCSDL dng Entity bean.

    Nhn phi chut ln Project, chn New->Others, chn MDN nh hnh

    t tn MDB l LophocMessage, gi l vovanhai.wordpress.com.mdb.

    Code cho phng thc onMessage

    public void onMessage(Message aMessage) {try {

    //y gi thuyt chng ta chly MapMessage

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    6/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    if (aMessage instanceof MapMessage) {MapMessage msg = (MapMessage) aMessage;

    Context ctx = new InitialContext();LophocLocalHome home = (LophocLocalHome) ctx.lookup("LophocBean");String msLop = msg.getString("msLop");String tenLop = msg.getString("tenLop");

    String gvcn = msg.getString("gvcn");LophocLocal lophoc = home.create(msLop, tenLop, gvcn);System.out.println("__________Insert class \"" + tenLop + "\" successful.");

    }} catch (Exception e) {

    e.printStackTrace();}

    }

    Cu trc Project

    Ni dung ejb-jar.xml

    jdbc:sqlserver://localhost:1433;databaseName=StudentMS [sa on

    dbo]LophocEBLophocBeanvovanhai.wordpress.com.entities.LophocLocalHomevovanhai.wordpress.com.entities.LophocLocalvovanhai.wordpress.com.entities.LophocBeanContainerjava.lang.Stringfalse

    Lophoc

    msLop

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    7/12

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    8/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    LophocBean*Required

    LophocMessageBean*

    Required

    Destination for LophocMessageMDBSampleServerQueue

    Ni dung file jboss.xml

    LophocBeanLophocBean

    LophocMessageBeanqueue/SampleServerQueue

    Ni dung file jbosscmp-jdbc.xml

    java:/Students_DSNMS SQLSERVERfalsefalse

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    9/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    LophocBeanjava:/Students_DSNMS SQLSERVERfalse

    LophocmsLopmsLop

    tenLoptenLop

    giaovienCNgiaovienCN

    Trin khai

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    10/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Client

    To 1 Web Application c tn EJB21_MDB_Entity_client

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    11/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    Thm 1 servlet c tn QueueClientServlet trong gi vovanhai.wordpress.com.vn nh sau:

    Code cho phn xl

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {

    System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

    System.setProperty("java.naming.provider.url", "127.0.0.1:1099");Context context = new InitialContext();QueueConnectionFactory connectionFactory = (QueueConnectionFactory)

    context.lookup("ConnectionFactory");QueueConnection queueConnection = connectionFactory.createQueueConnection();QueueSession queueSession = queueConnection.createQueueSession(false,

    Session.AUTO_ACKNOWLEDGE);Queue queue = (Queue) context.lookup("queue/TyteoDest");QueueSender queueSender = queueSession.createSender(queue);

  • 7/31/2019 Bai 12- Message Driven Bean - Entity Beans

    12/12

    Series: EJB 2.x vi Netbeans

    By V Vn Hihttp://vovanhai.wordpress.com

    MapMessage message = queueSession.createMapMessage();message.setString("msLop", "DHTH1A");message.setString("tenLop", "Lp i hc tin hc 1A");message.setString("gvcn", "V Vn Hi");

    queueSender.send(message);queueConnection.close();

    out.println("
    Queue send successful!...");} catch (Exception ex) {out.println(ex.getMessage());

    } finally {out.close();

    }}

    Kt qutrn Jboss console:

    Database :