sms4j v3.3.5

repository·master·Indexed 22 days ago

https://github.com/dromara/sms4j

A Java library for integrating multiple SMS providers into Spring Boot environments. It features a unified SmsFactory for sending messages across different vendors such as Alibaba Cloud, Huawei Cloud, and Zutong, and includes a configurable asynchronous SMS thread pool for managing sending tasks.

Tokens
1.6K
Snippets
4
Records
4
Agent score
29%

What's inside sms4j

  1. Integrate sms4j into a Spring Boot environment

    master

    To use sms4j in a Spring Boot project, follow these three steps:

    1. Add the Maven dependency: Include the sms4j-spring-boot-starter in your pom.xml.

    2. Configure providers in application.yml: Define your SMS providers under the sms.blends key. Each provider is identified by a custom unique key (e.g., 自定义标识1).

    3. Use the SmsFactory to send messages: Retrieve a specific provider instance using SmsFactory.getSmsBlend("your-custom-id") and call .sendMessage(phoneNumber, content).

    <dependency>
     <groupId>org.dromara.sms4j</groupId>
     <artifactId>sms4j-spring-boot-starter</artifactId>
     <version>最新版本</version>
    </dependency>
  2. Configure SMS provider blends in YAML

    master

    The sms.blends configuration allows you to define multiple SMS providers (blends) with different credentials and settings. Each blend is mapped to a unique identifier used in your code.

    Example: Alibaba Cloud (Aliyun) Configuration

    sms:
       config-type: yaml
       blends:
           AliyunProvider:
             accessKeyId: 您的accessKey
             accessKeySecret: 您的accessKeySecret
             signature: 测试签名
             templateId: SMS_215125134
             templateName: code
             requestUrl: dysmsapi.aliyuncs.com

    Example: Huawei Cloud Configuration

    sms:
       config-type: yaml
       blends:
           HuaweiProvider:
             appKey: 5N6fvXXXX920HaWhVXXXXXX7fYa
             app-secret: Wujt7EYzZTBXXXXXXEhSP6XXXX
             signature: 华为短信测试
             sender: 8823040504797
             template-id: acXXXXXXXXc274b2a8263479b954c1ab5
             statusCallBack: 
             url: https://XXXXX.cn-north-4.XXXXXXXX.com:443

    Example: Zutong (助通) Configuration

    sms:
       config-type: yaml
       blends:
           ZutongProvider:
             accessKeyId: tusxxxxxxXXX
             accessKeySecret: UbXXXxxx
             signature: 上海千XXXX
    sms:
       config-type: yaml
       blends:
           自定义标识1:
             #阿里云的accessKey
             accessKeyId: 您的accessKey
             #阿里云的accessKeySecret
             accessKeySecret: 您的accessKeySecret
             #短信签名
             signature: 测试签名
             #模板ID 用于发送固定模板短信使用
             templateId: SMS_215125134
             #模板变量 上述模板的变量
             templateName: code
             #请求地址 默认为dysmsapi.aliyuncs.com 如无特殊改变可以不用设置
             requestUrl: dysmsapi.aliyuncs.com
           自定义标识2:
             #华为短信appKey
             appKey: 5N6fvXXXX920HaWhVXXXXXX7fYa
             #华为短信appSecret
             app-secret: Wujt7EYzZTBXXXXXXEhSP6XXXX
             #短信签名
             signature: 华为短信测试
             #通道号
             sender: 8823040504797
             #模板ID 如果使用自定义模板发送方法可不设定
             template-id: acXXXXXXXXc274b2a8263479b954c1ab5
             #华为回调地址,如不需要可不设置或为空
             statusCallBack:
             #华为分配的app请求地址
             url: https://XXXXX.cn-north-4.XXXXXXXX.com:443
           自定义标识3:
             #助通短信
             accessKeyId: tusxxxxxxXXX
             accessKeySecret: UbXXXxxx
             signature: 上海千XXXX
  3. Configure the asynchronous SMS thread pool

    master

    Since different SMS vendors have varying support for asynchronous sending, sms4j provides a unified internal thread pool to handle asynchronous tasks. You can customize the following properties under the sms prefix in your YAML configuration:

    PropertyDescription
    corePoolSizeCore number of threads in the pool
    maxPoolSizeMaximum number of threads in the pool
    queueCapacityCapacity of the task queue
    keepAliveSecondsTime to keep idle threads alive
    threadNamePrefixPrefix for thread names
    shutdownStrategyIf true, waits for all tasks to complete before destroying beans during shutdown

    Default Configuration:

    sms:
      corePoolSize: 10
      maxPoolSize: 30
      queueCapacity: 50
      keepAliveSeconds: 60
      threadNamePrefix: sms-executor-
      shutdownStrategy: true
    sms:
      #核心线程池大小
      corePoolSize: 10
      #最大线程数
      maxPoolSize: 30
      #队列容量
      queueCapacity: 50
      #活跃时间
      keepAliveSeconds: 60
      # 线程名字前缀
      threadNamePrefix: sms-executor-
      #设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
      shutdownStrategy: true
  4. Send SMS messages using SmsFactory

    master

    Once configured, you can send SMS messages by accessing the desired provider through SmsFactory. This allows you to switch between different vendors (like Alibaba Cloud or Huawei Cloud) simply by changing the identifier passed to getSmsBlend.

    @RestController
    @RequestMapping("/test/")
    public class DemoController {
    
        // Test sending fixed template SMS
        @RequestMapping("/")
        public void send() {
             // Sends via the provider identified as '自定义标识1' (e.g., Alibaba Cloud)
            SmsFactory.getSmsBlend("自定义标识1").sendMessage("18888888888","123456");
            
            // Sends via the provider identified as '自定义标识2' (e.g., Huawei Cloud)
            SmsFactory.getSmsBlend("自定义标识2").sendMessage("16666666666","000000");
        }
    }
    @RestController
    @RequestMapping("/test/")
    public class DemoController {
    
        // 测试发送固定模板短信
        @RequestMapping("/")
        public void send() {
             // 阿里云向此手机号发送短信
            SmsFactory.getSmsBlend("自定义标识1").sendMessage("18888888888","123456");
            // 华为短信向此手机号发送短信
            SmsFactory.getSmsBlend("自定义标识2").sendMessage("16666666666","000000");
        }
    }