Snapper Snapper
首页
文档
  • 权限演示 (opens new window)
  • 工作流演示 (opens new window)
问答
更新日志
gitee (opens new window)
首页
文档
  • 权限演示 (opens new window)
  • 工作流演示 (opens new window)
问答
更新日志
gitee (opens new window)
  • 介绍及配置

    • 项目介绍
    • 微服务版项目配置
    • 项目开发
    • 权限使用
    • 权限的默认规则
    • 系统注解及上下文清单
    • 登录模式
    • 高级查询与注入
    • 状态流使用
    • 其他系统工具使用
      • 发邮件
      • http请求调用
      • fastdfs 文件上传
      • disruptor 生产者消费者
  • 集成使用
  • 介绍及配置
frog
2023-08-10
目录

其他系统工具使用

# 其他系统工具使用

# 发邮件

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....");
    }
});
        

# http请求调用

snapper-http包提供了http请求调用功能,请求调用的代码片段为

String result = IHttpClient.create().buildGet().sendXRaw(url, buildHeader());

# 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;
}

# 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]));
状态流使用

← 状态流使用

Theme by Vdoing | Copyright © 2023-2025
Frog

鲁ICP备2023023334号-1

鲁公网安备 37021302001133号

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式