1 / 23

Topics: JMS & JavaMail

Topics: JMS & JavaMail. Chin-Yi Tsai. JMS. JMS 提供一種可以在 J2EE 程式和元件間傳送訊息的方式 Message agent J2EE 應用程式和元件使用 JMS API 和 JMS 溝通 JMS 由五個元素所組成 Provider Client Message Administered object: designation factory, connection Native server. 訊息傳遞架構. Point-to-point 同步 Subscriber/publisher.

Download Presentation

Topics: JMS & JavaMail

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topics:JMS & JavaMail Chin-Yi Tsai

  2. JMS • JMS提供一種可以在J2EE程式和元件間傳送訊息的方式 • Message agent • J2EE應用程式和元件使用JMS API和JMS溝通 • JMS由五個元素所組成 • Provider • Client • Message • Administered object: designation factory, connection • Native server

  3. 訊息傳遞架構 • Point-to-point • 同步 • Subscriber/publisher

  4. Point-to-Point Messaging Msg Msg consumes Client1 Client2 Queue sends acknowledges

  5. Publish/Subscribe Messaging subscribes Topic Client2 Msg Client1 delivers publishes subscribes Client3 delivers

  6. The basic building blocks of a JMS application • Administered objects • connection factories and destinations • Connections • Sessions • Message producers • Message consumers • Messages

  7. JMS API Programming Model Connection Factory creates Connection Message Producer Message Consumer creates creates Session receives from sends to creates Destination Destination Msg

  8. JMS Message Types Message Format Message Header Message Properties Message Body

  9. javax.jms Package • Connection • Encapsulates a virtual connection with a JMS API provider • Session • Single-threaded context for producing and consuming messages • QueueSender • An object created by a session used for sending messages to a queue • QueueReceiver • An object created by a session used for receiving messages from a queue

  10. Creating a Point-to-Point JMS API Application 1. Look up a Connection factory using the J.N.D.I. API. 2. Look up the message queue using the J.N.D.I. API. 3. Create a Connection using the factory. 4. Create a Session object. 5. Create a MessageSender object. 6. Create one or more Message objects. 7. Send one or more Message objects using the MessageSender object. 8. Send a control message to the Queue object that allmessages have been sent.

  11. try { INitialContext jnidiContext = new InitialContext(); queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup( "QueueConnectionFactory" ); queue = (Queue) jndiContext.lookup( queueName ); queueConnection = queueConnectionFactory.createQueueConnection( ); queueSession = queueConnection.createQueueSession( false , Session.AUTO_ACKNOWLEDGE ); queueSender = queueSession.createSender( queue ); message = queueSession.createTextMessage( ); message.setText( "This is a simple message” ); queueSender.send( message ); queueConnection.close( ); } catch (JMSException e) { System.out.println("Exception occurred: " + } Send message

  12. try { InitialContext jndiContext = new InitialContext(); factory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); queue = (Queue) jndiContext.lookup(queueName); QueueConnection connection = factory.createQueueConnection (); QueueSession session = connection.createQueueSession(false, QueueSession.CLIENT_ACKNOWLEDGE ); receiver = session.createReceiver(queue); receiver.setMessageListener (new MessageListener(){ public void onMessage (Message newMessage){ try { TextMessage message = (TextMessage) newMessage; System.out.println("Message received "); System.out.println( message.getText() ); message.acknowledge ( ); } catch (Exception e) {} } }); connection.start(); } catch (JMSException e){ } catch (NamingException e) { } Receive message

  13. Creating a Publish/Subscribe JMS API Application 1. Look up a TopicConnection factory using the J.N.D.I. API. 2. Look up a Topic object using the J.N.D.I. API. 3. Create Connection and Session objects. 4. Create a TopicPublisher object. 5. Create one or more Message objects. 6. Publish one or more messages using the TopicPublisher object.

  14. try { topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); topic = (Topic) jndiContext.lookup(topicName); topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topicPublisher = topicSession.createPublisher(topic); message = topicSession.createTextMessage(); message.setText("This is a simple publish/subscribe message”); topicPublisher.publish(message); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } Publisher

  15. try { TopicConnectionFactory factory =(TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); topic = (Topic) jndiContext.lookup(topicName); TopicConnection connection = factory.createTopicConnection (); TopicSession session = connection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE ); subscriber = session.createSubscriber(topic); subscriber.setMessageListener (new MessageListener(){ public void onMessage (Message newMessage){ try { TextMessage message = (TextMessage) newMessage; System.out.println("Message received "); System.out.println( message.getText() ); message.acknowledge (); } catch (Exception e) {} } }); connection.start(); } catch (JMSException e){ } Subscriber

  16. JavaMail

  17. JavaMail API • 傳送電子郵件 • 接收電子郵件 • 刪除電子郵件 • 回覆和發送一封電子郵件 • 發送電子郵件 • 傳送附加檔案 • 接收附加檔案 • 搜索一個電子郵件資料夾

  18. Java Mail API Package • javax.mail • Classes modeling a mail system. • javax.mail.event • Listeners and events for the JavaMail API. • javax.mail.internet • Classes specific to Internet mail systems. • javax.mail.search • Message search terms for the JavaMail API.

  19. Important Classes • javax.mail.Session • Javax.mail.Message • Javax.mail.Address • Javax.mail.Authenticator • Javax.mail.Transport • Javax.mail.Store • Javax.mail.Folder

  20. Main Java mail main classes 收 送 Connection to server Session Receiving mail usingPOP or IMAP Store Connection toa remove mail folder(mainly the INBOX) Transport Sending mailusing SMTP Folder Sending a message Message Receive and arrayof messages

  21. 傳送電子郵件 Session Session sendMailSession;Store store;Transport transport; Properties props = new Properties(); sendMailSession = Session.getInstance(props, null); props.put("mail.smtp.host", "smtp.jspinsider.com"); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(request.getParameter("from"))); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ( request.getParameter ("to")));newMessage.setSubject(request.getParameter("subject"));newMessage.setSentDate(new Date());newMessage.setText(request.getParameter("text")); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); Message Transport

  22. 接收電子郵件 Session Properties props = new Properties( ); Session ses1 = Session.getDefaultInstance( props , null ); Store store1 = ses1.getStore(“pop3”); Store1.connect( host, username, password); Folder folder1 = store1.getFolder(“INBOX”); Folder1.open(Folder.READ_ONLY); Message msg[] = folder1.getMessage(); folder1.close(false); store1.close(); Store Folder Message

  23. reference • http://java.sun.com/products/javamail/javadocs/index.html • http://java.sun.com/j2ee/1.4/docs/api/

More Related