0% found this document useful (0 votes)
35 views23 pages

Topics: Jms & Javamail

The document discusses Java Message Service (JMS) and JavaMail API. JMS provides a way for J2EE applications and components to send messages. It consists of providers, clients, messages, administered objects like connection factories, and native servers. Messages can be sent point-to-point or publish/subscribe. The JavaMail API allows sending, receiving, deleting, and searching emails. It includes classes for sessions, messages, addresses, transports, stores, and folders. The document provides examples of creating JMS producers/consumers and sending/receiving emails.

Uploaded by

nalluri_08
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views23 pages

Topics: Jms & Javamail

The document discusses Java Message Service (JMS) and JavaMail API. JMS provides a way for J2EE applications and components to send messages. It consists of providers, clients, messages, administered objects like connection factories, and native servers. Messages can be sent point-to-point or publish/subscribe. The JavaMail API allows sending, receiving, deleting, and searching emails. It includes classes for sessions, messages, addresses, transports, stores, and folders. The document provides examples of creating JMS producers/consumers and sending/receiving emails.

Uploaded by

nalluri_08
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

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

2
訊息傳遞架構
Point-to-point
 同步

Subscriber/publisher

3
Point-to-Point Messaging

Msg
Msg
consumes
Client1 Queue Client2
sends
acknowledges

4
Publish/Subscribe Messaging

subscribes

Msg Client2

delivers
Client1
publishes Topic subscribes
Client3
delivers

5
The basic building blocks of a JMS application
Administered objects
 connection factories and destinations

Connections

Sessions

Message producers

Message consumers

Messages

6
JMS API Programming Model

Connection
Factory
creates

Connection

Message creates creates Message


Producer Session Consumer
sends to receives
creates from

Destination Msg Destination


7
JMS Message Types
Message Type Contains Some Methods
TextMessage String getText,setText

MapMessage set of name/value pairs setString,setDouble,setLon


g,getDouble,getString
BytesMessage stream of uninterpreted writeBytes,readBytes
bytes
StreamMessage stream of primitive values writeString,writeDouble,writ
eLong,readString
ObjectMessage serialize object setObject,getObject

Message Format
Message Header
Message Properties
Message Body
8
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

9
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.

10
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

11
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){ } Receive message
catch (NamingException e) { }
12
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.
13
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

14
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

15
JavaMail

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

17
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.

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

19
Main Java mail main classes
收 送

Receiving mail using Connection to server


POP or IMAP Session

Store

Connection to Sending mail


a remove mail folder Transport using SMTP
(mainly the INBOX)

Folder
Folder
Sending a message

Receive and array


of messages Messag
Messag
ee
20
傳送電子郵件
Session sendMailSession; Session
Store store;
Transport transport;

Properties props = new Properties();


Message
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
transport.send(newMessage);
21
接收電子郵件
Session

Properties props = new Properties( );


Session ses1 = Session.getDefaultInstance( props , null );

Store
Store store1 = ses1.getStore(“pop3”);

Store1.connect( host, username, password);


Folder
Folder folder1 = store1.getFolder(“INBOX”);
Folder1.open(Folder.READ_ONLY);

Message msg[] = folder1.getMessage(); Message

folder1.close(false);
store1.close();

22
reference
http://java.sun.com/products/javamail/javadocs/ind
ex.html

http://java.sun.com/j2ee/1.4/docs/api/

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy