1 线程池pool?
线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。
线程池可以帮助用户创建一个管理所有线程的容器,线程的创建和销毁都由线程池来管理。
spring线程池配置
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心线程数 --> <property name="corePoolSize" value="${task.core_pool_size}" /> <!-- 最大线程数 --> <property name="maxPoolSize" value="${task.max_pool_size}" /> <!-- 队列最大长度 --> <property name="queueCapacity" value="${task.queue_capacity}" /> <!-- 线程池维护线程所允许的空闲时间,默认为60s --> <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" /> </bean> |
task.core_pool_size=2 task.max_pool_size=10 task.queue_capacity=10 task.keep_alive_seconds=60 |
测试线程池
package com.www.thread;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/** * @author Sunshine * @description测试线程池
* 2017-12-20上午8:41:19 */ publicclass ThreadPoolTest {
/** * @param args */ publicstaticvoid main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); ThreadPoolTaskExecutor tt=(ThreadPoolTaskExecutor) ac.getBean("taskExecutor"); for (int i = 0; i < 50; i++) { MultiThreadByRunnable mb = new MultiThreadByRunnable("172.16.22."+i); tt.execute(mb); } }
}
|
2 使用多线程实现邮件批量发送。
如何使用java实现邮件发送
首先引入所需的jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> |
创建mail实体类类
package com.www.commons.mail;
/** * @author Sunshine * @description邮件实体类 * 2017-12-20上午9:26:20 */ publicclass MailModel {
private String from; private String to; private String subject; private String content; public String getFrom() { returnfrom; } publicvoid setFrom(String from) { this.from = from; } public String getTo() { returnto; } publicvoid setTo(String to) { this.to = to; } public String getSubject() { returnsubject; } publicvoid setSubject(String subject) { this.subject = subject; } public String getContent() { returncontent; } publicvoid setContent(String content) { this.content = content; } public MailModel( String to, String subject, String content) { super(); this.to = to; this.subject = subject; this.content = content; } public MailModel() { super(); // TODO Auto-generated constructor stub }
}
|
创建mail业务类
package com.www.commons.mail;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service;
/** * @author Sunshine * @description实现邮件发送 * 2017-12-20上午9:27:39 */ @Service("mailBiz") publicclass MailBiz { @Autowired private JavaMailSender mailSender; @Autowired private SimpleMailMessage simpleMailMessage; //注入线程池对象 @Autowired private ThreadPoolTaskExecutor taskExecutor;
/** * 发送邮件 */ public void sendMail(final MailModel mm) { taskExecutor.execute(new Runnable() {
@Override publicvoid run() { try { /*simpleMailMessage.setFrom(mm.getFrom());//设置发件人 */ simpleMailMessage.setTo(mm.getTo());//设置收件人 simpleMailMessage.setSubject(mm.getSubject());//主题 simpleMailMessage.setText(mm.getContent());//内容 mailSender.send(simpleMailMessage); } catch (Exception e) { System.out.println("邮件发送异常"); e.printStackTrace(); } System.out.println("邮件发送成功!");
} });
}
}
|
测试类
package com.www.mail;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.www.commons.mail.MailBiz; import com.www.commons.mail.MailModel;
publicclass SendMailTest {
/** * @param args */ publicstaticvoid main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); MailBiz mb=(MailBiz) ac.getBean("mailBiz"); MailModel mm = new MailModel( "1580496969@qq.com", "xxxxxxxxxxx", "ooooooooo"); mb.sendMail(mm); }
}
|
配置文件
#SMTP服务配置 mail.host=smtp.qq.com mail.port=465 mail.username=1234567@qq.com ##qq邮箱必须使用ssl授权码 mail.password=保密,自己申请 mail.smtp.auth=true mail.smtp.timeout=30000 mail.default.from=1234567@qq.com #STARTTLS是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL) mail.smtp.starttls.enable=true <!-- Spring提供的发送电子邮件的高级抽象类 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}" /> <property name="username" value="${mail.username}" /> <property name="password" value="${mail.password}" /> <property name="port" value="${mail.port}" /> <property name="defaultEncoding" value="UTF-8"></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop> <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop> <prop key="mail.smtp.socketFactory.port">${mail.port}</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.socketFactory.fallback">false</prop> </props> </property> </bean>
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from"> <value>${mail.default.from}</value> </property> </bean> |