ppgjx 发表于 2023-1-31 23:49

spring boot使用socket会被阻塞请求?

package com.api.task;

import com.api.dao.ApiUserDao;
import com.api.dao.IpInfoDao;
import com.api.dao.UserIpInfoDao;
import com.api.entity.*;
import com.api.service.ApiUserService;
import com.api.socket.server.RemoteServer;
import com.api.task.info.ProxyUserInfo;
import com.github.monkeywie.proxyee.server.HttpProxyServer;
import com.github.monkeywie.proxyee.server.HttpProxyServerConfig;
import com.github.monkeywie.proxyee.server.accept.HttpProxyAcceptHandler;
import com.github.monkeywie.proxyee.server.auth.model.BasicHttpToken;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.net.*;
import java.util.*;


* @Date: 2021/10/14/15:59
* @Description: 每日日报定时推送
*/


@Component
public class TestTask implements ApplicationRunner {

    @Value("${proxy_port}")
    private int port;

    @Override
    public void run(ApplicationArguments args){

      new Thread(new Runnable() {
            @Override
            public void run() {
                RemoteServer remoteServer = new RemoteServer(port);
                remoteServer.listen();
                System.out.println("哈哈");
            }
      }).start();

    }



    @PreDestroy
    public void backData(){
      System.out.println("结束端口");
    }



}


比如我在spring boot启动后新建了一个线程 去监听一个socket 服务器
public class RemoteServer {

    private final static Logger logger = LoggerFactory.getLogger(RemoteServer.class);


    private final int port;

    public RemoteServer(int port) {
      this.port = port;
    }

    public void listen() {
      try {
            logger.info("remote server start listening...");
            ServerSocket serverSocket = new ServerSocket(port);
            while (true) {
                Socket socket = serverSocket.accept();
                new RemoteServerThread(socket).start();
            }
      } catch (Exception e) {
            logger.error("", e);
      }
    }

}


监听以后RemoteServerThread里面的run方法就是不断地读取InputStream流,然后我发现如果给InputStream里面的打个断点,我springboot所有的接口都被阻塞了,这是啥原因了,而定时任务@Scheduled(cron = "0/60 * * * * *") debug就不会阻塞

mirakyux 发表于 2023-2-1 09:24

Suspend 由 All 改为 Thread
页: [1]
查看完整版本: spring boot使用socket会被阻塞请求?