带有JPA NullPointerException的springboot websocket

时间:2017-05-16 12:00:25

标签: spring-mvc spring-boot nullpointerexception spring-data-jpa spring-websocket

我正在使用一个SpringBoot Web服务使用websocket向客户端发送消息并使用JPA来保存数据。 在项目中我使用线程来创建数据,然后使用JPA来保存它,使用Websocket来显示数据。但它不适用于NullPointExecption。 这是我的代码:

@RestController
@ServerEndpoint("/websocket")
public class CrawlingController {
    private final Logger logger = LoggerFactory.getLogger(CrawlingController.class);
    @Autowired
    private SimulateDataRepository simulateDataRepository;


    private static GlobalStatus globalStatus = GlobalStatus.getInstance();

    private Session session;
    private static CopyOnWriteArraySet<CrawlingController> crawlingControllers =
            new CopyOnWriteArraySet<>();

    @OnOpen
    public void onOpen(Session session) throws Exception {
        this.session = session;
        crawlingControllers.add(this);
        String message = dataCrawling();

    }

    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
        for (CrawlingController controller : crawlingControllers)
            sendMessage(message);
    }

    @OnClose
    public void onClose() {
        crawlingControllers.remove(this);
        if (crawlingControllers.size() == 0)
            end();
       session=null;
       end();
    }

    private void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    public void onError(Session session, Throwable error) {
        logger.error(error.getMessage());
        System.out.println("websocket found something error!");
    }


    @RequestMapping("/start")
    public String dataCrawling() throws Exception {
        if (!globalStatus.isDataCrawling) {
            Process process = null;
            synchronized (globalStatus) {
                process = Utils.crawling();
                globalStatus.crawlingProcss = process;
                globalStatus.isDataCrawling = true;
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            new Thread(() -> {
                try {
                    stuffSimulate(bufferedReader);
                } catch (IOException | ParseException e) {
                    logger.error(e.getMessage());
                }
            }).start();
            return "start Finish!";
        } else return "faild";
    }

    public void stuffSimulate(BufferedReader reader) throws IOException, ParseException {
        line = reader.readLine();
        while (globalStatus.isDataCrawling) {
                    SimulateData simulateData = new SimulateData();
                    .... ...
                    simulateData.setData(data.toString());
                    this.saveAndShow(simulateData);
                }
            } 
        }
    }

    private void saveAndShow(SimulateData simulateData) throws IOException {
        simulateData = simulateDataRepository.saveAndFlush(simulateData);
        SummaryData summaryData = SimulateService.obtainSummary(simulateData);
        onMessage(summaryData.printSummary(),this.session);


        summaryData.sout();
    }
}
当我使用http://localhost:8080/start时,

simulateDataRepository可以正常工作。 mysql可以获取数据。 但是当我使用websocket时它不起作用! 错误meaasge说:

Exception in thread "Thread-5" java.lang.NullPointerException
    at cn.linhos.controller.CrawlingController.saveAndShow(CrawlingController.java:293)
    at cn.linhos.controller.CrawlingController.stuffSimulate(CrawlingController.java:172)
    at cn.linhos.controller.CrawlingController.lambda$dataCrawling$0(CrawlingController.java:104)
    at java.lang.Thread.run(Thread.java:748)

为什么simulateDataRepository在websocket时不能saveAndFlush但是当我使用“/ start”时可以这样做? 我该怎么做才能使websocket工作?

0 个答案:

没有答案