64 lines
1.6 KiB
Java
64 lines
1.6 KiB
Java
package com.czlisinterface.system.socket;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.Map;
|
|
|
|
@Slf4j
|
|
public class SocketUtils {
|
|
public static String socketServerPort;
|
|
|
|
/**
|
|
* 启动socket
|
|
*/
|
|
public static void socketStart(){
|
|
socketServerPort = getYmlValues("socketServerPort");
|
|
MLLPBasedHL7ThreadedServer server = new MLLPBasedHL7ThreadedServer(Integer.parseInt(socketServerPort), 3000);
|
|
server.setUpConnectionHandlers(); //启动线程
|
|
server.acceptConnections(); //创建socket服务
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取配置文件的值
|
|
* @param key
|
|
* @return
|
|
*/
|
|
public static String getYmlValues(String key) {
|
|
String value = "";
|
|
Yaml yaml = new Yaml();
|
|
InputStream inputStream = null;
|
|
String basePath= Thread.currentThread().getContextClassLoader().getResource("application.yml").getPath();
|
|
try {
|
|
inputStream = new FileInputStream(basePath);
|
|
Map map = (Map) yaml.load(inputStream);
|
|
value = String.valueOf(map.get(key));
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* 创建一个socket客户端并发送消息
|
|
*/
|
|
public static String sendMessage(String str){
|
|
try {
|
|
String s = SimpleTCPEchoClient.simpleClient(str);
|
|
return s;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
}
|