@@ -18,8 +18,21 @@ public class NtfyConnectionImpl implements NtfyConnection {
1818 private final ObjectMapper mapper = new ObjectMapper ();
1919
2020 public NtfyConnectionImpl () {
21- Dotenv dotenv = Dotenv .load ();
22- hostName = Objects .requireNonNull (dotenv .get ("HOST_NAME" ));
21+ String loadedHostName = null ;
22+ try {
23+ Dotenv dotenv = Dotenv .load ();
24+ loadedHostName = dotenv .get ("HOST_NAME" );
25+ } catch (Exception e ) {
26+ System .err .println ("WARNING: Could not load .env file for HOST_NAME. Using fallback." );
27+ }
28+
29+ this .hostName = (loadedHostName != null )
30+ ? loadedHostName
31+ : "http://localhost:8080" ;
32+
33+ if (this .hostName .equals ("http://localhost:8080" )) {
34+ System .out .println ("DEBUG: NtfyConnectionImpl running in test/fallback mode." );
35+ }
2336 }
2437
2538 public NtfyConnectionImpl (String hostName ) {
@@ -30,21 +43,23 @@ public NtfyConnectionImpl(String hostName) {
3043 public boolean send (String message ) {
3144 HttpRequest httpRequest = HttpRequest .newBuilder ()
3245 .POST (HttpRequest .BodyPublishers .ofString (message ))
33- //.header("Cache", "no")
3446 .uri (URI .create (hostName + "/mytopic" ))
3547 .build ();
36- try {
37- //Todo: handle long blocking send requests to not freeze the JavaFX thread
38- //1. Use thread send message?
39- //2. Use async?
40- var reponse = http .send (httpRequest , HttpResponse .BodyHandlers .discarding ());
41- return true ;
42- } catch (IOException e ) {
43- System .out .println ("Error sending message" );
44- } catch (InterruptedException e ) {
45- System .out .println ("Interruped sending message" );
46- }
47- return false ;
48+
49+ http .sendAsync (httpRequest , HttpResponse .BodyHandlers .discarding ())
50+ .thenAccept (response -> {
51+ if (response .statusCode () >= 200 && response .statusCode () < 300 ) {
52+ System .out .println ("Message sent successfully." );
53+ } else {
54+ System .err .println ("Error while sending: " + response .statusCode ());
55+ }
56+ })
57+ .exceptionally (e -> {
58+ System .err .println ("Network issue: " + e .getMessage ());
59+ return null ;
60+ });
61+
62+ return true ;
4863 }
4964
5065 @ Override
0 commit comments