其他系统工具使用
# 其他系统工具使用
# 发邮件
snapper-email包提供了邮件发送功能,一段发送邮件的代码片段为
SmtpServer smtpServer = MailServer.create()
.host((Checker.BeEmpty(bo.getProtocol()) ? SMTP_PREFIX : bo.getProtocol() + Strings.DOT) + bo.getHost())
.auth(bo.getAddress(), bo.getPasswd())
.ssl(true)
.buildSmtpMailServer();
if (Checker.BeNotEmpty(attchments)) {
for (File file : attchments) {
if (file.length() > SystemConsts.MAX_MAIL_FILE_SIZE) {
throw new ServerRuntimeException(ExceptionEnum.EMAIL_ATTCHMENT_SIZE_TOO_LARGE, SystemConsts.MAX_MAIL_FILE_SIZE / (1024 * 1024));
}
mail.embeddedAttachment(EmailAttachment.with().content(file));
}
}
//发送邮件
MailThreadPool pool = new MailThreadPool();
pool.newMailSenderThread(smtpServer, mail, new EmailSenderListener() {
@Override
public void onException(Email mail, String filePath, MailException exception) {
log.error("email attchments send fail, subject is {}, from is {}, exception is {}", mail.subject(), mail.from(), exception.getMessage());
}
@Override
public void onComplete(Email mail, String filePath) {
log.info("Email has sended....");
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# http请求调用
snapper-http包提供了http请求调用功能,请求调用的代码片段为
String result = IHttpClient.create().buildGet().sendXRaw(url, buildHeader());
1
# fastdfs 文件上传
snapper-oss-starter封装了fastdfs文件的上传、删除等操作,上传的代码片段为
@Autowired private DFSUpLoader dfsUploader;
public UploadResultWrap uploadFile(MultipartFile file) throws IOException {
UploadResultWrap result = new UploadResultWrap();
if (Checker.BeNull(file)) {
throw new ServerRuntimeException(ExceptionEnum.FILE_NOT_EXIST);
}
dfsUploader.upload(file.getOriginalFilename(), file.getInputStream(), new FileUploadListener() {
@Override
public void operationProgresse(long current, long total) {
}
@Override
public void operationException(DFSUploadException ex) {
}
@Override
public void operationComplete(StoredDFSFile dfsFile) {
result.setGroupName(dfsFile.getGroup()).setRemotePath(dfsFile.getPath()).setExtName(dfsFile.getExtName()).setUid(SnowflakeIdWorker.getId()).setName(dfsFile.getFileName()).setStatus("done").setUrl(dfsFile.getReadUrl());
}
});
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# disruptor 生产者消费者
snapper-core-starter提供了基于disruptor的生产者消费者模式,代码片段为
List<ObjectEvent<String>> events = Lists.newArrayList("xxx1","yyy2");
List<EventDisruptorConsumer<String>> consumers = Lists.newArrayList();
consumers.add(new EventDisruptorConsumer<String>() {
@Override
public void consume(String t) {
//your handle logic
}
});
//发布事件
EventDisruptorQueueFactory.publishSubscribeEvent(events, consumers.toArray(new EventDisruptorConsumer[0]));
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10