You are on page 1of 12

Series: EJB 2.

x vi Netbeans

Message-Driven Bean (MDB)


Trong bi ny, ta th lm 1 v d v MDB cho php nhn cc loi Message t client v x l 1 cch n gin: Trong netbeans, to 1 JavaEE project c tn EJB21_MDB_Ex01

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Nhn phi chut ln project va to, chn New->Other, chon JavaEE nh hnh

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Bn thm 1 Queue mi c tn TyteoQueue nh sau bng cch nhn nt Add tn from.

Nhn finish, bn c kt qu: By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Tin hnh vit MDB. y ti nhn 1 message gi n, ty vo n thuc 1 trong 5 kiu message no s x l n. Bn vit nt phn x l nh public void onMessage(Message aMessage) { try { if (aMessage instanceof TextMessage) { TextMessage msg = (TextMessage) aMessage; System.out.println("___________"+msg); } else if (aMessage instanceof MapMessage) { MapMessage msg = (MapMessage) aMessage; //c th x l theo yu cu ca bn String id = msg.getString("id"); String psw = msg.getString("psw"); System.out.println("___________received from client:\n\t"+ "id: "+id+"\n\tpassword:"+psw); } else if (aMessage instanceof BytesMessage) { BytesMessage msg = (BytesMessage) aMessage; byte[] data = new byte[1024];// di do bn qui nh msg.readBytes(data); //x l data } else if (aMessage instanceof StreamMessage) { StreamMessage msg = (StreamMessage) aMessage; //Bn c th c tng ng cc gi tr m bn a vo stream trc // v d: double val=msg.readDouble(); } else if (aMessage instanceof ObjectMessage) { ObjectMessage msg = (ObjectMessage) aMessage; //Lu trc th i tng bn gi phi m bo l implements Serializable Object obj=msg.getObject(); //X l obj } } catch (Exception ex) { ex.printStackTrace(); } } Thm 1 MDB khc c tn l MenMessage test trn topic

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Code cho phng thc onMessage ging nh code ca OnMessage phn trc Trin khai MDB ny trn JBOSS. Kt qu s xut hin 1 s li do u tin ta khng c topic no c tn MenTopic, Jboss s thng bo li v to cho chng 1 temporary topic c tn MenTopic. Bn c th l nloi64 ny hoc bn c th cu hnh n cho khi li. Cu hnh nh sau: Trong th mc %JBOSS_HOME%\ \server\default\deploy\jms\, m file jbossmq-destinationsservice.xml trong 1 trnh son tho text bt k (NotePad++ chng hn). Thm phn on xml sau <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=TyteoDest"> <depends optional-attributename="DestinationManager">jboss.mq:service=DestinationManager</depends> </mbean> thm 1 Queue c tn TyteoDest <mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=MenTopic"> <depends optional-attributename="DestinationManager">jboss.mq:service=DestinationManager</depends> <depends optional-attributename="SecurityManager">jboss.mq:service=SecurityManager</depends> </mbean> thm 1 Topic c tn MenTopic By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans Lu li, bn s thy c 1queue v 1 topic trong container

OK. By gi bn c th vit bt k client no send n MDB

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Client
To client web application c tn: EJB21_MDB_Ex01_client

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Thm 1 servlet c tn: TextMessageServlet

Code cho phn x l: 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); By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans TextMessage message = queueSession.createTextMessage(); message.setText("Hello from Queue Messaging"); queueSender.send(message); queueConnection.close(); out.println("<br/> Queue send successful!..."); } catch (Exception ex) { out.print(ex.getMessage()); } finally { out.close(); } Kt qu :

Thm 1 servlet c tn: MapMessageServlet

Code x l : By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans 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); MapMessage message=queueSession.createMapMessage(); message.setString("id", "Nguyn vn To"); message.setString("psw", "mypassword"); queueSender.send(message); queueConnection.close(); out.println("<br/> Queue send successful!..."); } catch (Exception ex) { out.print(ex.getMessage()); } finally { out.close(); } } Kt qu :

Thm 1 servlet c tn TestMessageServlet_Topic

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

Code x l nh sau : protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { InitialContext jndiContext = new InitialContext(); TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); Topic topic = (Topic) jndiContext.lookup("topic/MenTopic"); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher topicPublisher = topicSession.createPublisher(topic); TextMessage message = topicSession.createTextMessage(); topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT); for (int i = 0; i < 5; i++) { message.setText("<msg>This is message " + (i + 1) + "</msg>"); out.println("<br/>Publishing message: " + message.getText()); topicPublisher.publish(message); } topicConnection.close(); } catch (Exception ex) { out.println(ex.getMessage()); } finally { out.close(); } } Thc thi, kt qu nh sau :

By V Vn Hi http://vovanhai.wordpress.com

Series: EJB 2.x vi Netbeans

V 1 on kt qu trong ca s console ca Jboss.

By V Vn Hi http://vovanhai.wordpress.com

You might also like