相关文章:
- Spring参考手册 1 Spring Framework简介和典型的Web应用程序
- Spring参考手册 2 核心技术
- Spring参考手册 3 校验,数据绑定和类型转换
- Spring参考手册 4 面向切面编程
- Spring参考手册 5 数据访问
- Spring参考手册 6 网络
一、在Spring中使用远程调用
Spring集成了多个可以远程调用的技术。远程调用支持开发远程服务,用普通的POJO即可实现。目前Spring支持以下几个远程调用技术:
- Remote Method Invocation (RMI)
- Spring HTTP invoker
- Hessian
- Burlap
- Java Message Service(JMS),在底层使用JMS作为协议
- Advanced Message Queuing Protocol(AMQP),在底层使用AMQP作为协议
Spring自动发现功能不适用于远程接口,主要是为了避免对远程调用者开启太多的入口。
当讨论Spring的远程调用能力时我们使用以下实体类和相应的services:
public class Account implements Serializable{
private String name;
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface AccountService {
public void insertAccount(Account account);
public List<Account> getAccounts(String name);
}
public class AccountServiceImpl implements AccountService {
public void insertAccount(Account acc) {
// do something...
}
public List<Account> getAccounts(String name) {
// do something...
}
}
使用RMI的就不翻译了,性能不太好。只翻译一个Hessian。Burlap与Hessian配置方式一样,只需要把Hessian这个词换成Burlap就行了。AMQP看这里。关于它Spring另外有个项目。
1.1 使用Hessian通过HTTP远程调用services
Hessian提供了一个二进制基于HTTP的远程调用协议。更多信息请查看。
Hessian通过HTTP沟通,我们可以自定义一个servlet。你可以轻松的写出如下这样的一个servlet来曝露你的服务。
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
然后在WEB-INF
文件夹创建一个remoting-servlet.xml
配置文件
1.1.1 使用HessianServiceExporter曝露你的beans
我们将会在remoting-servlet.xml
配置文件中创建一个HessianServiceExporter
来曝露你的服务:
<bean id="accountService" class="example.AccountServiceImpl"></bean>
<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
现在我们去准备在客户端连接服务。没有显式的处理器映射被指定,那么BeanNameUrlHandlerMapping
将会被使用,因此,这个服务将会曝露在DispatcherServlet
的映射下并且以bean名称(也就是/AccountService
)命名的URL:http://HOST:8080/remoting/AccountService
1.1.2 在客户端连接服务
使用HessianProxyFactoryBean
我们可以在客户端链接服务。SimpleObject
使用AccountService
来管理账户:
<bean class="example.SimpleObject">
<property name="accountService" ref="accountService"/>
</bean>
<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
1.2 对使用Hessian或者Burlap曝露的服务应用HTTP基本身份验证
Hessian 和 Burlap的协议都是基于HTTP的,你可以很轻松的使用HTTP基本身份验证,例如:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors" ref="authorizationInterceptor"/>
</bean>
<bean id="authorizationInterceptor"
class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor">
<property name="authorizedRoles" value="administrator,operator"/>
</bean>
在这个应用程序上下文中这个拦截器只允许administrators 和 operators这两个角色调用bean方法。
具体文档也没说,有兴趣可以自行搜索,Apache Shiro也可以使用。
二、在Spring中使用邮件
首先添加第三方依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
org.springframework.mail
包是Spring Framework email支持的根级别包。发送邮件的核心接口是MailSender
;SimpleMailMessage
类封装了封装一个简单邮件的属性例如发件人、收件人。 这个包还提供了一个统一的邮件异常MailException
。
org.springframework.mail.javamail.JavaMailSender
接口添加了JavaMail的特性,例如MIME信息支持。JavaMailSender
还为JavaMail MIME消息的准备活动提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator
。
2.1 用法
我们假设这里有一个业务接口叫做OrderManager
:
public interface OrderManager {
void placeOrder(Order order);
}
2.1.1 MailSender 和 SimpleMailMessage的基本用法
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class SimpleOrderManager implements OrderManager {
private MailSender mailSender;
private SimpleMailMessage templateMessage;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setTemplateMessage(SimpleMailMessage templateMessage) {
this.templateMessage = templateMessage;
}
public void placeOrder(Order order) {
// Do the business calculations...
// Call the collaborators to persist the order...
// 创建一个线程安全的template message副本并且自定义它
SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
msg.setTo(order.getCustomer().getEmailAddress());
msg.setText(
"Dear " + order.getCustomer().getFirstName()
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is "
+ order.getOrderNumber());
try{
this.mailSender.send(msg);
}
catch (MailException ex) {
// 简单的输出它,然后继续
System.err.println(ex.getMessage());
}
}
}
下面是一些bean定义:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.mycompany.com"/>
</bean>
<!-- 这是一个template message,使用默认状态预加载它 -->
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- 发件人 -->
<property name="from" value="customerservice@mycompany.com"/>
<!-- 邮件主题 -->
<property name="subject" value="Your order"/>
</bean>
<bean id="orderManager" class="com.mycompany.businessapp.support.SimpleOrderManager">
<property name="mailSender" ref="mailSender"/>
<property name="templateMessage" ref="templateMessage"/>
</bean>
2.1.2 使用JavaMailSender 和 MimeMessagePreparator
这是另一个OrderManager
实现,使用的了MimeMessagePreparator
回调接口。请注意mailSender
参数是JavaMailSender
类型,这样我们便可使用JavaMail的MimeMessage
类:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
public class SimpleOrderManager implements OrderManager {
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void placeOrder(final Order order) {
// Do the business calculations...
// Call the collaborators to persist the order...
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(order.getCustomer().getEmailAddress()));
mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
mimeMessage.setText(
"Dear " + order.getCustomer().getFirstName() + " "
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is "
+ order.getOrderNumber());
}
};
try {
this.mailSender.send(preparator);
}
catch (MailException ex) {
// 简单的输出它,然后继续
System.err.println(ex.getMessage());
}
}
}
更多信息请参考JavaMail的javadoc。
2.1.3 使用JavaMail MimeMessageHelper
// 在现实中这里是通过依赖注入获得的
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("test@host.com");
helper.setText("Thank you for ordering!");
sender.send(message);
发送附件与内联资源
附件好理解,内联资源举个例子可以是图片或者Excel显式在你的邮件正文中而不是作为一个附件显示。
附件
下面这个例子展示了如何使用MimeMessageHelper
来发送一封邮件并且使用一张图片作为附件:
// 在现实中这里是通过依赖注入获得的
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
// 使用true标识指明你需要一个multipart信息
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test@host.com");
helper.setText("Check out this image!");
// 让我们添加一个附件
FileSystemResource file = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addAttachment("CoolImage.jpg", file);
sender.send(message);
内联资源
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test@host.com");
// use the true flag to indicate the text included is HTML
helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);
// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);
sender.send(message);
一个基于Velocity的例子
要使用Velocity来创建你的邮件模版,你首先要将Velocity的类库引入。
创建模版页面:
# in the com/foo/package
<html>
<body>
<h3>Hi ${user.userName}, welcome to the Chipping Sodbury On-the-Hill message boards!</h3>
<div>
Your email address is <a href="mailto:${user.emailAddress}">${user.emailAddress}</a>.
</div>
</body>
</html>
配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.csonth.gov.uk"/>
</bean>
<bean id="registrationService" class="com.foo.SimpleRegistrationService">
<property name="mailSender" ref="mailSender"/>
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
</beans>
package com.foo;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;
public class SimpleRegistrationService implements RegistrationService {
private JavaMailSender mailSender;
private VelocityEngine velocityEngine;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
public void register(User user) {
// Do the registration logic...
sendConfirmationEmail(user);
}
private void sendConfirmationEmail(final User user) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmailAddress());
message.setFrom("webmaster@csonth.gov.uk"); // could be parameterized...
Map model = new HashMap();
model.put("user", user);
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "com/dns/registration-confirmation.vm", model);
message.setText(text, true);
}
};
this.mailSender.send(preparator);
}
}
未完,待续...
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.