22
33import com .greenapi .client .pkg .api .GreenApi ;
44import lombok .RequiredArgsConstructor ;
5+ import lombok .SneakyThrows ;
56import lombok .extern .log4j .Log4j2 ;
67import org .springframework .stereotype .Component ;
78
9+ import java .sql .Time ;
810import java .util .Objects ;
911
1012@ Component
@@ -17,25 +19,31 @@ public class WebhookConsumer {
1719
1820 private boolean running = true ;
1921
22+ @ SneakyThrows
2023 public void start (WebhookHandler webhookHandler ) {
2124 running = true ;
2225
2326 while (running ) {
24- var response = greenApi .receiving .receiveNotification ();
27+ try {
28+ var response = greenApi .receiving .receiveNotification ();
2529
26- if (Objects .equals (response .getBody (), "null" )) {
27- log .info ("receiveNotification timeout" );
30+ if (Objects .equals (response .getBody (), "null" )) {
31+ log .info ("receiveNotification timeout" );
2832
29- } else {
30- var notification = notificationMapper .get (response .getBody ());
31-
32- if (notification .getBody () == null ) {
33- log .error ("Can't map webhook from json!" );
34- greenApi .receiving .deleteNotification (notification .getReceiptId ());
3533 } else {
36- webhookHandler .handle (notification );
37- greenApi .receiving .deleteNotification (notification .getReceiptId ());
34+ var notification = notificationMapper .get (response .getBody ());
35+
36+ if (notification .getBody () == null ) {
37+ log .error ("Can't map webhook from json!" );
38+ greenApi .receiving .deleteNotification (notification .getReceiptId ());
39+ } else {
40+ webhookHandler .handle (notification );
41+ greenApi .receiving .deleteNotification (notification .getReceiptId ());
42+ }
3843 }
44+ } catch (Exception e ) {
45+ log .error ("UnexpectedError: " + e );
46+ Thread .sleep (5000 );
3947 }
4048 }
4149 }
0 commit comments