当前位置: 首页 > news >正文

唐山论坛建站模板深圳网络品牌推广公司

唐山论坛建站模板,深圳网络品牌推广公司,商城开发方案,wordpress手机导航三横拦解决 怎么解决,就是 日志别输出到 cmd 就行了。就行了。就行了。 java -jar demo.jar > output.log 2>&1 &最近写东西,遇到了 程序偶尔卡死的情况。是java -jar 启动的。具体卡死为:http请求超级卡顿 或 偶尔反应好多个请求&…

解决

  • 怎么解决,就是 日志别输出到 cmd 就行了。就行了。就行了。
java -jar demo.jar > output.log 2>&1 &
  • 最近写东西,遇到了 程序偶尔卡死的情况。是java -jar 启动的。
  • 具体卡死为:http请求超级卡顿 或 偶尔反应好多个请求,或 tcp Server 粘包,无法收取消息。
  • 直到:我晃了晃 cmd 窗口,最大化,最小化,程序竟然动了,日志又输出了。
  • 终于终于明白了,Window cmd 即要保证正常输出,又要保证性能不卡死你的程序
  • 那你的程序又输出了很多很多日志,那cmd窗口怎么办呢,只有卡死你的程序了。

怀疑流程

  • 因为遇到的是 http请求 和 tcp server 或同时能用,同时不能用,或只能用一个。
  • 怀疑1:是 bio tcp server出现了问题,用了 nio 还一样,疯了。
  • 怀疑2:spring boot 或 spring框架的问题 或 开线程等等问题,最终各种启动 config配置类,配置bean。在 main方法。使用postConstruct 等等等
  • 最后还是没找到问题
    //ApplicationContext context//@PostConstruct//@Async@EventListener(ApplicationReadyEvent.class)/*@Componentpublic class TcpServerListener implements ApplicationListener<WebServerInitializedEvent> {@Autowiredprivate UpdateServer updateServer;@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {//int serverPort = event.getWebServer().getPort();// 在此处添加代码以创建和启动 TCP 服务器updateServer.startServer(8082);}}*/

BIO

//@Component
public class UpdateServer {/*@SpringBootApplicationpublic class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}@EventListener(ApplicationReadyEvent.class)public void startServerSocket() throws IOException {// 创建ServerSocket并绑定端口ServerSocket serverSocket = new ServerSocket(8080);// 启动一个新线程来接受客户端连接new Thread(() -> {while (true) {try {// 接受客户端连接Socket clientSocket = serverSocket.accept();// 处理客户端请求,可以在这里实现自己的逻辑// 关闭客户端连接clientSocket.close();} catch (IOException e) {e.printStackTrace();}}}).start();}}*//*@Componentpublic class TcpServerListener implements ApplicationListener<WebServerInitializedEvent> {@Autowiredprivate UpdateServer updateServer;@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {//int serverPort = event.getWebServer().getPort();// 在此处添加代码以创建和启动 TCP 服务器updateServer.startServer(8082);}}*//*** TCP升级网关*///@Resource//private TcpUpdateMapper tcpUpdateMapper;/*** Redis*///@Resource//private RedisUtil redisUtil;//new Thread(() -> { }).start();//@PostConstruct//@Async/*public void startServer() throws Exception {//开启TCP ServerServerSocket serverSocket = new ServerSocket(8082);log.info("服务端打开了 Server started on port {}", 8082);while (true) {//等待连接Socket socket = serverSocket.accept();//获取 输入流DataInputStream inputStream = new DataInputStream(socket.getInputStream());DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());log.info("服务端收到了连接:Accepted connection from client: {}", socket.getInetAddress());//读取的字节数byte[] buffer = new byte[380];//1024//真正读取到了多少个int bytesRead;//循环进行读取while ((bytesRead = inputStream.read(buffer)) != -1) {//处理二进制数据processBinaryData(buffer, bytesRead, socket, outputStream);}//循环读取,正常每次都会读取完毕inputStream.close();outputStream.close();socket.close();}//死循环接收连接}//初始化方法结束*///关闭输入流//socket.shutdownInput();//关闭输出流,关闭 Socket//socket.shutdownOutput();//关闭Socket//socket.close();

NIO

@Configuration
public class TcpServerConfig {/*** TCP升级网关*/@Resourceprivate TcpUpdateMapper tcpUpdateMapper;/*** Redis*/@Resourceprivate StringRedisTemplate stringRedisTemplate;@Value("${tcp.ip}")private String tcpIp;//ApplicationContext context//@PostConstruct//@Async@EventListener(ApplicationReadyEvent.class)public void startServer() {// throws IOException//异步执行//ThreadUtil.execAsync(() -> {try {//打开选择器Selector selector = Selector.open();//打开通道ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();//非阻塞serverSocketChannel.configureBlocking(false);//本机IPserverSocketChannel.bind(new InetSocketAddress(tcpIp, 8082));//进行注册serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);log.info("===============Server started on port 8082===================");while (true) {//打开通道int readyChannels = selector.select();//校验下if (readyChannels == 0) {continue;}//获取所有的keySet<SelectionKey> selectedKeys = selector.selectedKeys();//取得迭代器Iterator<SelectionKey> keyIterator = selectedKeys.iterator();//如果存在while (keyIterator.hasNext()) {//获取这个keySelectionKey key = keyIterator.next();//如果是个连接if (key.isAcceptable()) {//打开这个通道SocketChannel clientSocketChannel = serverSocketChannel.accept();//非阻塞的clientSocketChannel.configureBlocking(false);//注册为可读clientSocketChannel.register(selector, SelectionKey.OP_READ);log.info("收到连接:Accepted connection from: {}", clientSocketChannel.getRemoteAddress());} else if (key.isReadable()) {//如果是可读,获取通道SocketChannel clientSocketChannel = (SocketChannel) key.channel();//读取数量ByteBuffer buffer = ByteBuffer.allocate(38);//1024//真实读取到了多少个int bytesRead = -1;try {//这里报错了,增加防护,如果防护,这个nio就断了bytesRead = clientSocketChannel.read(buffer);} catch (Exception e) {log.info("未读取到");e.printStackTrace();}//未读取到 关闭if (bytesRead == -1) {clientSocketChannel.close();} else if (bytesRead > 0) {//读取到了buffer.flip();//读到这个字节里byte[] data = new byte[bytesRead];buffer.get(data);//处理逻辑processBinaryData(data, bytesRead, clientSocketChannel);//String message = new String(data).trim();//System.out.println("Received message: " + message);//清理buffer.clear();}//读到了}//是可读的//移除keykeyIterator.remove();}//如果存在}//死循环} catch (Exception e) {e.printStackTrace();}//}, "MyTcpServerThread").start();
}
    //有错误,往外层抛private void sendUpdateMessage(SocketChannel clientSocketChannel) {//去空格redisData = redisData.replace(" ", "");//转为bytebyte[] binaryData = hexStringToBytes(redisData);//申请字节ByteBuffer buffer = ByteBuffer.allocate(binaryData.length);//写入buffer.put(binaryData);buffer.flip();//循环写入while (buffer.hasRemaining()) {try {clientSocketChannel.write(buffer);log.info("发送了一次升级的消息");} catch (IOException e) {log.info("发送升级失败");e.printStackTrace();}}}
http://www.hkea.cn/news/884124/

相关文章:

  • 健身网站开发项目总结关键词筛选工具
  • 重庆网站建设零臻靠谱国内永久免费的云服务器
  • 软件库合集软件资料2024郑州百度快照优化
  • 房地产开发公司网站建设方案seo去哪里学
  • 做网站可以赚钱吗百度小说搜索风云排行榜
  • 做网站交接需要哪些权限网站seo视频教程
  • 在网站怎么做收款二维码刷移动关键词优化
  • 问信息奥赛题怎么做 去哪个网站互联网网络推广
  • b2c电子商务网站系统下载专业网站seo推广
  • 引流推广的方法seo诊断工具
  • 平阴县建设工程网站直通车推广怎么做
  • 网站开发外包不给ftp高佣金app软件推广平台
  • 太原适合网站设计地址百度用户服务中心客服电话
  • 济南源码网站建设长沙网站seo推广公司
  • 北京网站制作17页和业务多一样的平台
  • 无锡市住房城乡建设委网站简单网页设计模板html
  • 武汉市大型的网站制作公司网站ip查询
  • 做仪表行业推广有哪些网站电商网站设计
  • 动静分离网站架构百度售后客服电话24小时
  • 做汽车配件生意的网站佛山seo关键词排名
  • 创意建站推荐百度做广告多少钱一天
  • 巴中网站建设公司百度seo怎么做网站内容优化
  • 查网站备案名称上海网络营销seo
  • 人是用什么做的视频网站网络营销方案设计毕业设计
  • 建设网站考虑因素关键词优化是怎么弄的
  • 陕西营销型网站建设推广普通话的内容简短
  • 做配电箱的专门网站百度指数属于行业趋势及人群
  • 学做网站的网站重庆seo整站优化报价
  • 保定网站设计概述seo推广软件排名
  • 查pv uv的网站网络营销推广服务