Compare commits
No commits in common. '47d9d86be3ac44c165dc5af46192f5e3e02c452e' and '6c61f5bd06ac9b7afb552f8f7f9934b636bf2005' have entirely different histories.
47d9d86be3
...
6c61f5bd06
@ -1,32 +0,0 @@
|
||||
package com.mashibing.common.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPP2DeliverEnums
|
||||
* @Description: CMPP2.0协议-短信发送后回调枚举
|
||||
* @date 2025/6/17 17:56
|
||||
*/
|
||||
|
||||
@Getter
|
||||
public enum CMPP2DeliverEnums {
|
||||
|
||||
DELIVERED("DELIVRD", "Message is delivered to destination"),
|
||||
EXPIRED("EXPIRED", "Message validity period has expired"),
|
||||
DELETED("DELETED", "Message has been deleted."),
|
||||
UNDELIVERABLE("UNDELIV", "Message is undeliverable"),
|
||||
ACCEPTED("ACCEPTD", "Message is in accepted state"),
|
||||
UNKNOWN("UNKNOWN", "Message is in invalid state"),
|
||||
REJECTED("REJECTD", "Message is in a rejected state"),
|
||||
;
|
||||
|
||||
private String stat;
|
||||
|
||||
private String description;
|
||||
|
||||
CMPP2DeliverEnums(String stat, String description) {
|
||||
this.stat = stat;
|
||||
this.description = description;
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package com.mashibing.common.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPP2ResultEnums
|
||||
* @Description: CMPP2.0协议-短信提交后的应答枚举
|
||||
* @date 2025/6/17 17:32
|
||||
*/
|
||||
|
||||
@Getter
|
||||
public enum CMPP2ResultEnums {
|
||||
|
||||
OK(0, "正确"),
|
||||
MESSAGE_BUILD_ERROR(1, "消息结构错"),
|
||||
COMMAND_WORD_ERROR(2, "命令字错"),
|
||||
MESSAGE_SEQUENCE_ERROR(3, "消息序号重复"),
|
||||
MESSAGE_LENGTH_ERROR(4, "消息长度错"),
|
||||
INCORRECT_TARIFF_CODE(5, "资费代码错"),
|
||||
Exceeding_maximum_message_length(6, "超过最大信息长"),
|
||||
BUSINESS_CODE_ERROR(7, "业务代码错"),
|
||||
FLOW_CONTROL_ERROR(8, "流量控制错"),
|
||||
UNKNOWN(9, "其他错误");
|
||||
|
||||
private final Integer result;
|
||||
|
||||
private final String msg;
|
||||
|
||||
CMPP2ResultEnums(Integer result, String msg) {
|
||||
this.result = result;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package com.mashibing.common.utils;
|
||||
|
||||
import com.mashibing.common.enums.CMPP2DeliverEnums;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPP2DeliverUtil
|
||||
* @Description: CMPP2.0协议-短信发送后回调枚举工具类
|
||||
* @date 2025/6/17 17:59
|
||||
*/
|
||||
|
||||
public class CMPP2DeliverUtil {
|
||||
|
||||
private static Map<String, String> stats = new HashMap<>();
|
||||
|
||||
static {
|
||||
CMPP2DeliverEnums[] cmpp2DeliverEnums = CMPP2DeliverEnums.values();
|
||||
for (CMPP2DeliverEnums cmpp2DeliverEnum : cmpp2DeliverEnums) {
|
||||
stats.put(cmpp2DeliverEnum.getStat(), cmpp2DeliverEnum.getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过result结果拿到对应的错误信息
|
||||
*
|
||||
* @param stat CMPP2DeliverEnum.stat
|
||||
* @return CMPP2DeliverEnums.description
|
||||
*/
|
||||
public static String getResultMessage(String stat) {
|
||||
return stats.get(stat);
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package com.mashibing.common.utils;
|
||||
|
||||
import com.mashibing.common.enums.CMPP2ResultEnums;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPP2ResultUtil
|
||||
* @Description: CMPP2.0协议-短信提交后的应答枚举工具类
|
||||
* @date 2025/6/17 17:39
|
||||
*/
|
||||
|
||||
public class CMPP2ResultUtil {
|
||||
|
||||
private static Map<Integer, String> results = new HashMap<>();
|
||||
|
||||
static {
|
||||
CMPP2ResultEnums[] cmpp2ResultEnums = CMPP2ResultEnums.values();
|
||||
for (CMPP2ResultEnums cmpp2ResultEnum : cmpp2ResultEnums) {
|
||||
results.put(cmpp2ResultEnum.getResult(), cmpp2ResultEnum.getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过result结果拿到对应的错误信息
|
||||
*
|
||||
* @param result CMPP2ResultEnum.result
|
||||
* @return CMPP2ResultEnum.msg
|
||||
*/
|
||||
public static String getResultMessage(Integer result) {
|
||||
return results.get(result);
|
||||
}
|
||||
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package com.mashibing.common.utils;
|
||||
|
||||
import com.mashibing.common.pojo.StandardReport;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPPDeliverMapUtil
|
||||
* @Description: 临时存储StandardReport对象
|
||||
* @date 2025/6/17 18:01
|
||||
*/
|
||||
|
||||
public class CMPPDeliverMapUtil {
|
||||
|
||||
private static ConcurrentHashMap<String, StandardReport> tempReport = new ConcurrentHashMap<>();
|
||||
|
||||
public static void put(String msgId, StandardReport submit) {
|
||||
tempReport.put(msgId, submit);
|
||||
}
|
||||
|
||||
public static StandardReport get(String msgId) {
|
||||
return tempReport.get(msgId);
|
||||
}
|
||||
|
||||
public static StandardReport remove(String msgId) {
|
||||
return tempReport.remove(msgId);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.mashibing.common.utils;
|
||||
|
||||
import com.mashibing.common.pojo.StandardSubmit;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: CMPPSubmitRepoMapUtil
|
||||
* @Description: 临时存储StandardSubmit对象
|
||||
* @date 2025/6/15 17:07
|
||||
*/
|
||||
|
||||
public class CMPPSubmitRepoMapUtil {
|
||||
|
||||
private static ConcurrentHashMap<String, StandardSubmit> tempSubmit = new ConcurrentHashMap<>();
|
||||
|
||||
public static StandardSubmit put(int sequence, StandardSubmit standardSubmit) {
|
||||
return tempSubmit.put(sequence + "", standardSubmit);
|
||||
}
|
||||
|
||||
public static StandardSubmit get(int sequence) {
|
||||
return tempSubmit.get(sequence + "");
|
||||
}
|
||||
|
||||
public static StandardSubmit remove(int sequence) {
|
||||
return tempSubmit.remove(sequence + "");
|
||||
}
|
||||
|
||||
private CMPPSubmitRepoMapUtil() {
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.mashibing</groupId>
|
||||
<artifactId>beacon-cloud</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>beacon-smsgateway</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- nacos注册中心 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<!-- nacos配置中心 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
<!-- feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- AMQP -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<!-- 测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<!-- common公共组件 -->
|
||||
<dependency>
|
||||
<groupId>com.mashibing</groupId>
|
||||
<artifactId>beacon-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- netty4 -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
<!-- commons-lang3工具类 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<!-- hippo4j-client -->
|
||||
<dependency>
|
||||
<groupId>cn.hippo4j</groupId>
|
||||
<artifactId>hippo4j-spring-boot-starter</artifactId>
|
||||
<version>1.5.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,24 +0,0 @@
|
||||
package com.mashibing.smsgateway;
|
||||
|
||||
import cn.hippo4j.core.enable.EnableDynamicThreadPool;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: SmsGatewayApplication
|
||||
* @Description: SmsGatewayApplication网关启动类
|
||||
* @date 2025/6/13 14:32
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableDiscoveryClient
|
||||
@EnableDynamicThreadPool // hippo4j启动类注解 开启动态线程池
|
||||
public class SmsGatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SmsGatewayApplication.class, args);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.mashibing.smsgateway.config;
|
||||
|
||||
import org.springframework.amqp.core.AcknowledgeMode;
|
||||
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: RabbitMQConfig
|
||||
* @Description: 配置针对指定消费者的配置可以用配置类的方式
|
||||
* @date 2025/6/13 16:04
|
||||
*/
|
||||
|
||||
//@Configuration
|
||||
public class RabbitMQConfig {
|
||||
|
||||
// @Bean
|
||||
public SimpleRabbitListenerContainerFactory gatewayContainerFactory(ConnectionFactory connectionFactory,
|
||||
SimpleRabbitListenerContainerFactoryConfigurer configurer) {
|
||||
SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
|
||||
simpleRabbitListenerContainerFactory.setConcurrentConsumers(5);
|
||||
simpleRabbitListenerContainerFactory.setPrefetchCount(10);
|
||||
simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
|
||||
configurer.configure(simpleRabbitListenerContainerFactory, connectionFactory);
|
||||
return simpleRabbitListenerContainerFactory;
|
||||
}
|
||||
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package com.mashibing.smsgateway.config;
|
||||
|
||||
import cn.hippo4j.core.executor.DynamicThreadPool;
|
||||
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: ThreadPoolConfig
|
||||
* @Description: 动态线程池配置类
|
||||
* @date 2025/6/16 20:35
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class ThreadPoolConfig {
|
||||
|
||||
@Bean
|
||||
@DynamicThreadPool
|
||||
public ThreadPoolExecutor cmppSubmitPool() {
|
||||
// 短信提交应答
|
||||
String threadPoolId = "cmpp-submit";
|
||||
|
||||
return ThreadPoolBuilder.builder()
|
||||
// 指定线程名称的前缀
|
||||
.threadFactory(threadPoolId)
|
||||
// 线程池在Hippo4j中的唯一标识
|
||||
.threadPoolId(threadPoolId)
|
||||
// 代表动态线程池
|
||||
.dynamicPool()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@DynamicThreadPool
|
||||
public ThreadPoolExecutor cmppDeliverPool() {
|
||||
//短信状态报告应答
|
||||
String threadPoolId = "cmpp-deliver";
|
||||
|
||||
return ThreadPoolBuilder.builder()
|
||||
.threadFactory(threadPoolId)
|
||||
.threadPoolId(threadPoolId)
|
||||
.dynamicPool()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package com.mashibing.smsgateway.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* @author heqijun
|
||||
* @ClassName: TestController
|
||||
* @Description: 动态线程池测试类
|
||||
* @date 2025/6/17 16:57
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@Resource
|
||||
ThreadPoolExecutor cmppSubmitPool;
|
||||
|
||||
@Resource
|
||||
ThreadPoolExecutor cmppDeliverPool;
|
||||
|
||||
@GetMapping("test")
|
||||
public String test() {
|
||||
cmppSubmitPool.execute(() -> System.out.println("【" + LocalDateTime.now() + "】"
|
||||
+ Thread.currentThread().getName()));
|
||||
return "OK";
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4;
|
||||
|
||||
import com.mashibing.smsgateway.netty4.entity.CmppMessageHeader;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*/
|
||||
public class CMPPEncoder extends MessageToByteEncoder<Object> {
|
||||
|
||||
public CMPPEncoder(){
|
||||
super(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
|
||||
if (msg instanceof byte[]){
|
||||
out.writeBytes((byte[])msg);
|
||||
}else if(msg instanceof Integer){
|
||||
out.writeInt((Integer)msg);
|
||||
}else if(msg instanceof Byte){
|
||||
out.writeByte((Byte)msg);
|
||||
}else if(msg instanceof Long){
|
||||
out.writeLong((Long)msg);
|
||||
}else if(msg instanceof String){
|
||||
out.writeBytes(((String)msg).getBytes("UTF-16BE"));
|
||||
}else if (msg instanceof Character){
|
||||
out.writeChar((Character)msg);
|
||||
}else if (msg instanceof CmppMessageHeader){
|
||||
CmppMessageHeader c=(CmppMessageHeader)msg;
|
||||
out.writeBytes(c.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4;
|
||||
|
||||
|
||||
import com.mashibing.smsgateway.netty4.entity.CmppActiveTest;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
|
||||
|
||||
/**
|
||||
* 心跳Handler
|
||||
*/
|
||||
public class HeartHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private NettyClient client;
|
||||
public HeartHandler(NettyClient client){
|
||||
this.client=client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
System.out.println("初始化创建连接。。。");
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
IdleState state = event.state();
|
||||
if (state == IdleState.WRITER_IDLE || state == IdleState.ALL_IDLE) {
|
||||
client.submit(new CmppActiveTest());
|
||||
System.out.println("心跳启动!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
} else {
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelInactive(ctx);
|
||||
client.reConnect(10);
|
||||
}
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4;
|
||||
|
||||
import com.mashibing.smsgateway.netty4.entity.CmppConnect;
|
||||
import com.mashibing.smsgateway.netty4.entity.CmppMessageHeader;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.logging.LogLevel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
|
||||
|
||||
/**
|
||||
* 封装的netty客户端
|
||||
*/
|
||||
public class NettyClient {
|
||||
|
||||
// 通道
|
||||
private Channel channel;
|
||||
// IP
|
||||
private String host;
|
||||
// 端口
|
||||
private int port;
|
||||
// 用户名
|
||||
private String serviceId;
|
||||
// 密码
|
||||
private String pwd;
|
||||
|
||||
// 构建NettyClient
|
||||
public NettyClient(String host, int port, String serviceId, String pwd) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.serviceId = serviceId;
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
// 开启连接
|
||||
public void start() {
|
||||
try {
|
||||
doConnect(host, port);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 在保持连接的状态下提交信息
|
||||
public boolean submit(CmppMessageHeader submit) {
|
||||
if (isActive()) {
|
||||
channel.writeAndFlush(submit);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 是否保持连接
|
||||
public boolean isActive() {
|
||||
if (channel == null) {
|
||||
return false;
|
||||
}
|
||||
if (!channel.isOpen() || !channel.isActive() || !channel.isWritable()) {
|
||||
//channel没开 或 没激活
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重连机制 在 channelInactive 触发时调用
|
||||
*
|
||||
* @param reConnect
|
||||
*/
|
||||
public void reConnect(int reConnect) {
|
||||
int times = 0;
|
||||
while (true && times < reConnect) {
|
||||
try {
|
||||
if (!isActive()) {
|
||||
start();
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(10 * 1000);
|
||||
times++;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println("尝试重连...:" + host + ":" + port + " / " + serviceId);
|
||||
try {
|
||||
Thread.sleep(10 * 1000);
|
||||
times++;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立连接
|
||||
* @param host
|
||||
* @param port
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public void doConnect(final String host, final int port) throws InterruptedException {
|
||||
|
||||
//创建线程组 - 手动设置线程数,默认为cpu核心数2倍
|
||||
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
|
||||
//创建引导程序
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
//保持长连接
|
||||
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
//将线程加入bootstrap
|
||||
bootstrap.group(eventLoopGroup)
|
||||
.remoteAddress(host, port)
|
||||
//使用指定通道类
|
||||
.channel(NioSocketChannel.class)
|
||||
//设置日志
|
||||
.handler(new LoggingHandler(LogLevel.INFO))
|
||||
//重写通道初始化方法
|
||||
.handler(new NettyClientInitializer(this));
|
||||
|
||||
ChannelFuture channelFuture = bootstrap.connect().sync();
|
||||
|
||||
channel = channelFuture.channel();
|
||||
|
||||
//账号登陆
|
||||
CmppMessageHeader cmppConnect = new CmppConnect(serviceId, pwd);
|
||||
channel.writeAndFlush(cmppConnect);
|
||||
|
||||
channelFuture.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||
|
||||
//关闭前阻塞
|
||||
// channelFuture.channel().closeFuture().sync();
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class NettyStartCMPP {
|
||||
|
||||
//ip
|
||||
public static String host = "127.0.0.1";
|
||||
//端口
|
||||
public static int port = 7890;
|
||||
//账号
|
||||
public static String serviceId = "laozheng";
|
||||
//密码
|
||||
public static String pwd = "JavaLaoZheng123!";
|
||||
|
||||
|
||||
|
||||
@Bean(initMethod = "start")
|
||||
public NettyClient nettyClient() {
|
||||
NettyClient cmppClient = new NettyClient(host, port, serviceId, pwd);
|
||||
return cmppClient;
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4.entity;
|
||||
|
||||
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
public class CmppActiveTest extends CmppMessageHeader {
|
||||
|
||||
public CmppActiveTest() {
|
||||
super(Command.CMPP_ACTIVE_TEST, Command.CMPP2_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现类必须自定义对象序列化
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteBuf buf = Unpooled.buffer(12);
|
||||
buf.writeInt(12);
|
||||
buf.writeInt(Command.CMPP_ACTIVE_TEST);
|
||||
buf.writeInt(0);
|
||||
return buf.array();
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4.entity;
|
||||
|
||||
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
|
||||
public class CmppActiveTestResp extends CmppMessageHeader {
|
||||
public CmppActiveTestResp() {
|
||||
super(Command.CMPP_ACTIVE_TEST_RESP, Command.CMPP2_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现类必须自定义对象序列化
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteBuf buf = Unpooled.buffer(4 + 4 + 4 + 1);
|
||||
buf.writeInt(4 + 4 + 4 + 1);
|
||||
buf.writeInt(Command.CMPP_ACTIVE_TEST_RESP);
|
||||
buf.writeInt(0);
|
||||
buf.writeByte(0);
|
||||
return buf.array();
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4.entity;
|
||||
|
||||
import com.mashibing.smsgateway.netty4.utils.Command;
|
||||
import com.mashibing.smsgateway.netty4.utils.MsgUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
/**
|
||||
* 与CMPP建立连接需要的信息
|
||||
*/
|
||||
public class CmppConnect extends CmppMessageHeader {
|
||||
|
||||
private String serviceId;
|
||||
|
||||
private String pwd;
|
||||
|
||||
public CmppConnect(String serviceId, String pwd) {
|
||||
super(Command.CMPP_CONNECT, Command.CMPP2_VERSION);
|
||||
this.serviceId = serviceId;
|
||||
this.pwd = pwd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteBuf buf = Unpooled.buffer(4 + 4 + 4 + 6 + 16 + 1 + 4);
|
||||
|
||||
//Total_Length
|
||||
buf.writeInt(4 + 4 + 4 + 6 + 16 + 1 + 4);
|
||||
//Command_Id
|
||||
buf.writeInt(Command.CMPP_CONNECT);
|
||||
//Sequence_Id
|
||||
buf.writeInt(MsgUtils.getSequence());
|
||||
//sourceAddr
|
||||
buf.writeBytes(MsgUtils.getLenBytes(serviceId, 6));
|
||||
//authenticatorSource
|
||||
buf.writeBytes(MsgUtils.getAuthenticatorSource(serviceId, pwd));
|
||||
//version
|
||||
buf.writeByte(1);
|
||||
//timestamp
|
||||
buf.writeInt(Integer.parseInt(MsgUtils.getTimestamp()));
|
||||
|
||||
return buf.array();
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.mashibing.smsgateway.netty4.entity;
|
||||
|
||||
|
||||
import com.mashibing.smsgateway.netty4.utils.MsgUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
|
||||
/**
|
||||
* CMPP响应信息
|
||||
*/
|
||||
public class CmppSubmitResp {
|
||||
|
||||
private int sequenceId;
|
||||
|
||||
private int result;
|
||||
|
||||
private long msgId;
|
||||
|
||||
public CmppSubmitResp(byte[] bytes) {
|
||||
this.sequenceId = MsgUtils.bytesToInt(ArrayUtils.subarray(bytes, 8, 12));
|
||||
this.msgId = MsgUtils.bytesToLong(ArrayUtils.subarray(bytes, 12, 20));
|
||||
this.msgId = Math.abs(this.msgId);
|
||||
this.result = bytes[20];
|
||||
}
|
||||
|
||||
public int getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public long getMsgId() {
|
||||
return msgId;
|
||||
}
|
||||
|
||||
public int getSequenceId() {
|
||||
return sequenceId;
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
# 服务名称
|
||||
spring:
|
||||
application:
|
||||
name: beacon-smsgateway
|
||||
# 多环境
|
||||
profiles:
|
||||
active: dev
|
||||
# nacos注册中心地址
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 192.168.1.13:8848
|
||||
# nacos配置中心地址:
|
||||
config:
|
||||
server-addr: 192.168.1.13:8848
|
||||
file-extension: yml
|
||||
# beacon-smsgateway-dev.yml
|
||||
|
||||
# dynamic:
|
||||
# thread-pool:
|
||||
# server-addr: http://localhost:6691
|
||||
# username: admin
|
||||
# password: 123456
|
||||
# namespace: beacon-cloud
|
||||
# item-id: ${spring.application.name}
|
Loading…
Reference in new issue