SipSubscribe.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.genersoft.iot.vmp.gb28181.event;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.scheduling.annotation.Scheduled;
  5. import org.springframework.stereotype.Component;
  6. import javax.sip.*;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.Map;
  10. import java.util.concurrent.ConcurrentHashMap;
  11. @Component
  12. public class SipSubscribe {
  13. private final Logger logger = LoggerFactory.getLogger(SipSubscribe.class);
  14. private Map<String, SipSubscribe.Event> errorSubscribes = new ConcurrentHashMap<>();
  15. private Map<String, SipSubscribe.Event> okSubscribes = new ConcurrentHashMap<>();
  16. private Map<String, Date> timeSubscribes = new ConcurrentHashMap<>();
  17. // @Scheduled(cron="*/5 * * * * ?") //每五秒执行一次
  18. @Scheduled(cron="0 0 * * * ?") //每小时执行一次, 每个整点
  19. public void execute(){
  20. logger.info("[定时任务] 清理过期的订阅信息");
  21. Calendar calendar = Calendar.getInstance();
  22. calendar.setTime(new Date());
  23. calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) - 1);
  24. for (String key : timeSubscribes.keySet()) {
  25. if (timeSubscribes.get(key).before(calendar.getTime())){
  26. logger.info("[定时任务] 清理过期的订阅信息: {}", key);
  27. errorSubscribes.remove(key);
  28. okSubscribes.remove(key);
  29. timeSubscribes.remove(key);
  30. }
  31. }
  32. }
  33. public interface Event {
  34. void response(EventResult eventResult);
  35. }
  36. public static class EventResult<EventObject>{
  37. public int statusCode;
  38. public String type;
  39. public String msg;
  40. public String callId;
  41. public Dialog dialog;
  42. public EventObject event;
  43. public EventResult() {
  44. }
  45. public EventResult(EventObject event) {
  46. this.event = event;
  47. if (event instanceof ResponseEvent) {
  48. ResponseEvent responseEvent = (ResponseEvent)event;
  49. this.type = "response";
  50. this.msg = responseEvent.getResponse().getReasonPhrase();
  51. this.statusCode = responseEvent.getResponse().getStatusCode();
  52. this.callId = responseEvent.getDialog().getCallId().getCallId();
  53. this.dialog = responseEvent.getDialog();
  54. }else if (event instanceof TimeoutEvent) {
  55. TimeoutEvent timeoutEvent = (TimeoutEvent)event;
  56. this.type = "timeout";
  57. this.msg = "消息超时未回复";
  58. this.statusCode = -1024;
  59. this.callId = timeoutEvent.getClientTransaction().getDialog().getCallId().getCallId();
  60. this.dialog = timeoutEvent.getClientTransaction().getDialog();
  61. }else if (event instanceof TransactionTerminatedEvent) {
  62. TransactionTerminatedEvent transactionTerminatedEvent = (TransactionTerminatedEvent)event;
  63. this.type = "transactionTerminated";
  64. this.msg = "事务已结束";
  65. this.statusCode = -1024;
  66. this.callId = transactionTerminatedEvent.getClientTransaction().getDialog().getCallId().getCallId();
  67. this.dialog = transactionTerminatedEvent.getClientTransaction().getDialog();
  68. }else if (event instanceof DialogTerminatedEvent) {
  69. DialogTerminatedEvent dialogTerminatedEvent = (DialogTerminatedEvent)event;
  70. this.type = "dialogTerminated";
  71. this.msg = "会话已结束";
  72. this.statusCode = -1024;
  73. this.callId = dialogTerminatedEvent.getDialog().getCallId().getCallId();
  74. this.dialog = dialogTerminatedEvent.getDialog();
  75. }
  76. }
  77. }
  78. public void addErrorSubscribe(String key, SipSubscribe.Event event) {
  79. errorSubscribes.put(key, event);
  80. timeSubscribes.put(key, new Date());
  81. }
  82. public void addOkSubscribe(String key, SipSubscribe.Event event) {
  83. okSubscribes.put(key, event);
  84. timeSubscribes.put(key, new Date());
  85. }
  86. public SipSubscribe.Event getErrorSubscribe(String key) {
  87. return errorSubscribes.get(key);
  88. }
  89. public void removeErrorSubscribe(String key) {
  90. errorSubscribes.remove(key);
  91. timeSubscribes.remove(key);
  92. }
  93. public SipSubscribe.Event getOkSubscribe(String key) {
  94. return okSubscribes.get(key);
  95. }
  96. public void removeOkSubscribe(String key) {
  97. okSubscribes.remove(key);
  98. timeSubscribes.remove(key);
  99. }
  100. public int getErrorSubscribesSize(){
  101. return errorSubscribes.size();
  102. }
  103. public int getOkSubscribesSize(){
  104. return okSubscribes.size();
  105. }
  106. }