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

JMS & JavaMail for Developers

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)
71 views23 pages

JMS & JavaMail for Developers

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

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
[Link] 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)
[Link]( "QueueConnectionFactory" );
queue = (Queue) [Link]( queueName );
queueConnection =
[Link]( );
queueSession = [Link]( false ,
Session.AUTO_ACKNOWLEDGE );
queueSender = [Link]( queue );
message = [Link]( );
[Link]( "This is a simple message” );
[Link]( message );
[Link]( );

} catch (JMSException e) {

[Link]("Exception occurred: " +

}
Send message

11
try {
InitialContext jndiContext = new InitialContext();
factory = (QueueConnectionFactory)
[Link]("QueueConnectionFactory");
queue = (Queue) [Link](queueName);
QueueConnection connection = [Link] ();
QueueSession session = [Link](false,
QueueSession.CLIENT_ACKNOWLEDGE );
receiver = [Link](queue);

[Link] (new MessageListener(){


public void onMessage (Message newMessage){
try {
TextMessage message = (TextMessage) newMessage;
[Link]("Message received ");
[Link]( [Link]() );
[Link] ( );
} catch (Exception e) {}
}
});
[Link]();
}
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)
[Link]("TopicConnectionFactory");
topic = (Topic) [Link](topicName);
topicConnection = [Link]();
topicSession = [Link](false,
Session.AUTO_ACKNOWLEDGE);
topicPublisher = [Link](topic);
message = [Link]();
[Link]("This is a simple publish/subscribe message”);
[Link](message);

} catch (JMSException e) {

[Link]("Exception occurred: " + [Link]());

Publisher

14
try {
TopicConnectionFactory factory =(TopicConnectionFactory)
[Link]("TopicConnectionFactory");
topic = (Topic) [Link](topicName);
TopicConnection connection = [Link] ();
TopicSession session = [Link](false,
TopicSession.CLIENT_ACKNOWLEDGE );
subscriber = [Link](topic);

[Link] (new MessageListener(){


public void onMessage (Message newMessage){
try {
TextMessage message = (TextMessage) newMessage;
[Link]("Message received ");
[Link]( [Link]() );
[Link] ();
} catch (Exception e) {}
}
});

[Link]();

} catch (JMSException e){ } Subscriber

15
JavaMail

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

17
Java Mail API Package
[Link]
 Classes modeling a mail system.

[Link]
 Listeners and events for the JavaMail API.

[Link]
 Classes specific to Internet mail systems.

[Link]
 Message search terms for the JavaMail API.

18
Important Classes
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

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 = [Link](props, null);

[Link]("[Link]", "[Link]");

Message newMessage = new MimeMessage(sendMailSession);

[Link](new InternetAddress([Link]("from")));
[Link]([Link],
new InternetAddress ( [Link] ("to")));
[Link]([Link]("subject"));
[Link](new Date());
[Link]([Link]("text"));

transport = [Link]("smtp");
Transport
[Link](newMessage);
21
接收電子郵件
Session

Properties props = new Properties( );


Session ses1 = [Link]( props , null );

Store
Store store1 = [Link](“pop3”);

[Link]( host, username, password);


Folder
Folder folder1 = [Link](“INBOX”);
[Link](Folder.READ_ONLY);

Message msg[] = [Link](); Message

[Link](false);
[Link]();

22
reference
[Link]
[Link]

[Link]

23

You might also like