The content of this chapter comes from a real Wall Street Financial Website Project.
This chapter assumes readers to send through the JavaMail library through the SMTP (Simple Mail Transfer Protocol) server
Email the email, and have read the "Singleton mode" chapter. Run this chapter
Before the code is provided, please
Www.javasoft.com Download the latest version of the JavaMail library.
16.1 Questions & Solutions
problem
I believe that many readers have come into contact with the system that automatically sends an email. Everyone must know that you can use it.
The JavaMail library sends an email from an SMTP server. In general, a company will have several
SMTP server, although each server is regularly shut down maintenance inspection, but the company will always maintain at least one
The SMTP server runs normally in order to process the work of sending emails.
Suppose a company has a J2EE system to automatically send an email, and this work must be 24/7
OK, that is, it is all-weather. In other words, it cannot be stopped because a SMTP server is stopped. versus
At the same time, SMTP servers cannot be downtime maintenance. At this time, the system architect designer needs to solve this question.
question.
So the project manager invites several designers to the conference room and explains the problem and requires designers to provide possible
Design. After a short discussion, the designers put forward two options.
First program
Install a self-owned SMTP server, maintained this server by system maintenance personnel. This server is not necessarily
To deliver your email to your customers, it only needs to transfer your email to the SMTP that is running in the company.
The server is OK. Since the SMTP server can get a working SMTP service from the DNS server
The name of the device, so you can guarantee that the mail can always be sent.
This is of course possible, just maintaining a SMTP server is equal to adding additional maintenance work, and who can
Ensure that this server does not need to shut down and repair?
Java and mode · 250 ·
Second solution
The SMTP server (hereinafter referred to as the source server) that needs to be maintained (hereinafter referred to as the source server) in the scenario one.
The SMTP server is running (hereinafter referred to as the target server), and transfers the message to it because the source server can
Enough to use DNS directory services for directory queries. DNS will provide all registered SMTP services to the entire network
The name of the device, so that the source server can test all the names on the target server list until one is tested.
Target server running normally.
With Java's JNDI function, Java programs can do the same thing. This means it, you can write a Java
The program automatically gets the list of all SMTP servers registered within the DNS server (called
MX Record), then let it test all the servers on the list one by one, until you send the email. Public
The Division will always have at least one SMTP server is working properly, so that you can guarantee that you can send the message.
Further improvement in the second program
If you only need to write 20 lines of code, you can save the day-day server maintenance work, this is of course good.
Over. Thus, this program is listed as a preferred scheme to eventually get a perfect solution.
Below is a design plan reached by the various opinions proposed in a comprehensive discussion:
(1) Since JNDI and DNS need JNDI and DNS, then you may wish to download the latest libraries to Sun Microsystem's website.
(2) Do not do DNS queries every time you send mail, you should create a class responsible for querying and saving queries.
The result of the result.
(3) The MX record is a fairly static table, so the entire system only needs a table. The system needs to send an email at any time, just call this list to this object.
All of this points to the single sample mode.
Single example mode
You can't forget that this book is introduced to illustrate the use of single case mode, so it is necessary to take this opportunity to emphasize
Why should you use a single example mode here, and where you use a single case pattern here.
First, as designers are pointed out in the discussion, only such an MX record table is required throughout the system.
This is of course the most important reason to use single case mode.
Second, the operation of sending an email may be triggered at any time. That is, this object of saving the MX record table should
When there is a running period of time, it is not collected by the garbage collector. Readers who know the garbage collector may
Real estate, this requires maintaining at least one reference to it within the system. So who saves the reference to this object,
To ensure that it is not collected? Of course it is its best. In this way, as long as this single case object is created, it will always
It will not be collected until the server environment is restarted. This is another important thing to use the single sample model here.
reason.
Therefore, single example mode is most suitable for use in this system design.
Chapter 16 Special: Single Case Model and MX Record · 251 ·
In order to give readers who are not familiar with JNDI, DNS and MX records, a chance to familiarize with these concepts,
These concepts were reviewed before the line system design. Readers who have been familiar with these concepts can skip the following
The section is directly reading the "System Design" section.
16.2 Directory Services and MX Record
Name - Directory Service
Namedn in a life - An example of a directory service is the telephone directory query service provided by the phone station. This service will
Connect the names, addresses, and phones listed on the phone book.
Computer naming - Directory service is a basic tool for computer systems, and more strengthens than phone directory services
Big. Name-Directory Service Links an object with a name so that the customer can find this through the name of the object
Object. The directory service allows objects to have properties so that the client can be further pair after finding an object by querying.
The properties of the icon, or in turn according to the attribute query object.
The most common directory services include LDAP (Lightweight Directory Access Protocol) and DNS (Domain)
Name service).
What is DNS
DNS is domain service, computer users find other computer users on the network
It must be served by domain name. On the Internet and any network based on TCP / IP, every computer
There is a unique IP address (Internet Protocol Address). These IP addresses are like house numbers on the street.
Make other computers to find a computer. DNS servers provide DNS services.
MX record (MX RECORD)
MX (MX (MAIL Exchange) Record is a mail exchange record. Email Server Record Specifies a server
Receive an email of a domain name. That is, the message sent to JeffCorp.com will send to mail.jeffcorp.com's services
MX records that complete this task should be like this
Jeffcorp.com. in mx 10 mail.jeffcorp.com
The leftmost left side of this record is the email domain name used on the international network; the third column is a number
10. It represents the priority of this server is 10. Generally, a large system has several email servers.
These servers can be used as a candidate server in accordance with priority. Priority must be a positive integer, the lower the number, the higher the priority.
Java and mode · 252 ·
16.3 JNDI Architecture Introduction
JNDI's full name is Java Naming and Directory Interface, is in 1997
At the beginning of the year, it was introduced by Sun Microsystem, its purpose is to provide a generality of various directory types for Java systems.
Sex access interface.
The JNDI architecture consists of JNDI API and JNDI SPI. JNDI API makes a Java application to use
A series of named Naming and Directory services. JNDI SPI is a service provider, including a directory
Service providers are ready, it allows a variety of names and directory services to be inserted into the system in a transparent way.
Di. In the JNDI architecture diagram, several names and directory services are given as an example, as shown below.
JNDI API consists of four libraries below: