1 Star 8 Fork 4

Gitee 极速下载 / sms-integration

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/otary/sms-integration
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

sms-integration

基于SMGP3.4协议、CMPP协议的短信发送框架

jdk spring Codacy Badge Build Status

特性

  • 支持SMGP3.4协议、CMPP协议

文档

中文文档

依赖

<dependency>
    <groupId>cn.chenzw.sms</groupId>
    <artifactId>sms-core</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>cn.chenzw.sms</groupId>
    <artifactId>sms-spring</artifactId>
    <version>1.0</version>
</dependency>

SMGP协议

核心类

  • SMGPConnection

示例

简单代码

import cn.chenzw.sms.core.Connection;
import cn.chenzw.sms.core.Message;
import cn.chenzw.sms.core.Session;
import cn.chenzw.sms.core.protocol.smgp.SMGPConnection;

SMGPConnection conn = new SMGPConnection();
conn.setClientId("xxx");  // 帐号
conn.setPassword("yyy");   // 密码
conn.setVersion((byte) 0);
conn.setAutoReconnect(true);
conn.setSendInterval(200);

conn.connect("222.66.24.235", 8900); // 短信网关IP和端口

if (conn.isConnected()) {
    Session session = conn.getSession();
    try {
        for (int i = 0; i < 3; i++) {
            session.submit("第" + i + "条短信内容", "1065902100612", "18046048466");
        }
    } finally {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException ex) {}
        try {
            session.close();
        } catch (IOException ex) { }
    }
}

携带自定义ID并注册回调函数

SMGPConnection conn = new SMGPConnection();
conn.setClientId("xxx");
conn.setPassword("yyy");
conn.setHost("222.66.24.235");
conn.setPort(8900);
conn.setVersion((byte) 0);
conn.setAutoReconnect(true);
conn.setSendInterval(200);

conn.connect();

// 注册全局回调函数
conn.registerCallbackHandler(new ConnectionCallback() {
    @Override
    public void onLoginSuccess(Connection connection, Message message) {
        log.info("login success:" + message);
    }

    @Override
    public void onLoingFailure(Connection connection, Message message) {
        log.info("login failure:" + message);
    }

});
if (conn.isConnected()) {
    Session session = conn.getSession();

    // 注册会话级回调函数
    session.registerCallbackHandler(new SubmitCallback() {
        @Override
        public void onSendSuccess(Connection connection, Message message) {
            log.info("send success:{}", message);
        }

        @Override
        public void onSendFailure(Connection connection, Message message) {
            log.info("send failure:{}", message);
        }
    });

    try {
        session.submit("xxx001", "测试内容1", "1065902100612", "18046048466");
        session.submit("xxx002", "测试内容2", "1065902100612", "18046048466");
    } finally {
        try {
            Thread.sleep(3000L);
            session.close();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

与Spring集成

1)添加sms-spring依赖

<dependency>
	<groupId>cn.chenzw.sms</groupId>
    <artifactId>sms-integration</artifactId>
    <version>1.0</version>
</dependency>

2)注册SMGPConnectionFactoryBean

XML配置方式

<bean id="smgpConnection" class="cn.chenzw.sms.spring.protocol.smgp.SMGPConnectionFactoryBean">
   <property name="host" value="139.224.36.226"/>
   <property name="port" value="8890"/>
   <property name="clientId" value="XXXX"/>
   <property name="password" value="XXXX121"/>
</bean>

Java配置方式

@Configuration
public class AppConfig {

    @Bean
    public SMGPConnectionFactoryBean smgpConnectionFactoryBean() throws Exception {
        SMGPConnectionFactoryBean smgpConnectionFactoryBean = new SMGPConnectionFactoryBean();
        smgpConnectionFactoryBean.setHost("");
        smgpConnectionFactoryBean.setPassword("");
        smgpConnectionFactoryBean.setClientId("");
        smgpConnectionFactoryBean.setPort(8990);
        smgpConnectionFactoryBean.setVersion(0);
        smgpConnectionFactoryBean.setAutoReconnect(true);
        smgpConnectionFactoryBean.setSendInterval(200);
        return smgpConnectionFactoryBean;
    }
}

3)注入SMGPConnectionFactoryBean


@Autowired
SMGPConnection smgpConnection;

// ....

CMPP协议

核心类

  • CMPPConnection

示例

CMPPConnection conn = new CMPPConnection();
conn.setSourceAddr("123456");
conn.setPassword("aaa001");
conn.setVersion((byte) 0);
conn.setAutoReconnect(true);
conn.setSendInterval(200);

conn.connect("127.0.0.1", 7890);

if (conn.isConnected()) {
    Session session = conn.getSession();
    String[] phones = new String[]{"13162645136"};

    long startTime = System.currentTimeMillis();
    try {
        for (int i = 0; i < phones.length * 10; i++) {
            String content = String.format("第%d条:电信cmpp测试X(%s)", i + 1, format.format(new Date()));
            session.submit(content, "1065902100612", phones[i / 10]);

        }
    } finally {
        log.info(String.format("total:%d", System.currentTimeMillis() - startTime));
    }
}

Comments ( 1 )

Sign in for post a comment

About

sms-integration 是一个基于 SMGP3.4 协议、CMPP 协议的短信发送框架 spread retract
Cancel

Releases

No release

Contributors

All

Activities

load more
can not load any more
Java
1
https://gitee.com/mirrors/sms-integration.git
git@gitee.com:mirrors/sms-integration.git
mirrors
sms-integration
sms-integration
master

Search

101046 36f486cf 1899542 101055 4cb66026 1899542