postconstruct在springboot中停止了我的所有api

ybzsozfc  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(298)

我正在尝试在我的spring boot类中创建一个新线程,我希望它用我的spring boot启动,但在我启动服务器时它没有启动:
代码:

  1. public class TrashCanController implements DisposableBean, Runnable {
  2. @Autowired
  3. TrashCanRepository trashCanRepository;
  4. public static TrashCanResponse response;
  5. String Data;
  6. TrashCanResponse trashCanResponse = new TrashCanResponse();
  7. private Thread thread;
  8. private volatile boolean someCondition = true;
  9. @GetMapping("/DataTrashCan")
  10. public void getAllinfo() throws Exception {
  11. final boolean scanFinished = false;
  12. final int x = 1;
  13. final String hc06Url = "btspp://FCA87A00B212:1;authenticate=false;encrypt=false;master=false";
  14. final StreamConnection streamConnection = (StreamConnection) Connector.open(hc06Url);
  15. final BufferedReader input = new BufferedReader(new InputStreamReader(streamConnection.openInputStream()));
  16. try {
  17. while (true) {
  18. String inputLine = input.readLine();
  19. while (inputLine.length() == 1) {
  20. inputLine = input.readLine();
  21. }
  22. if (inputLine.length() >= 4 && inputLine.length() <= 13) {
  23. final String[] data = inputLine.split(",", 3);
  24. TrashCanResponse response = new TrashCanResponse(Integer.parseInt(data[0]), Integer.parseInt(data[1]),
  25. Integer.parseInt(data[2]));
  26. System.out.println(response.getDistance());
  27. System.out.println(response.getHumidity());
  28. System.out.println(response.getTemperature());
  29. Thread.sleep(10000);
  30. trashCanRepository.save(response);
  31. }
  32. }
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. @Override
  38. public void run() {
  39. while (someCondition) {
  40. try {
  41. getAllinfo();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. @Override
  48. public void destroy() throws Exception {
  49. someCondition = false;
  50. }

我正在使用@postconstruct,但当函数从服务器启动时,它停止了我的所有api。
那么怎样才能让它正常工作呢??

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题