You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2021/06/16 13:36:25 UTC

[tomee-chatterbox] 06/06: Adding parameters and logging

This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee-chatterbox.git

commit 6d150f94cd8f62ca94327b654c7c8d4aa2f28bed
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Wed Jun 16 14:35:24 2021 +0100

    Adding parameters and logging
---
 .../nats/adapter/NATSResourceAdapter.java          | 23 ++++++++++++++--------
 .../src/main/rar/META-INF/ra.xml                   |  8 ++++++++
 chatterbox-nats/docker-compose.yml                 |  2 +-
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/chatterbox-nats/chatterbox-nats-impl/src/main/java/org/apache/tomee/chatterbox/nats/adapter/NATSResourceAdapter.java b/chatterbox-nats/chatterbox-nats-impl/src/main/java/org/apache/tomee/chatterbox/nats/adapter/NATSResourceAdapter.java
index cfbbf41..2f52de6 100644
--- a/chatterbox-nats/chatterbox-nats-impl/src/main/java/org/apache/tomee/chatterbox/nats/adapter/NATSResourceAdapter.java
+++ b/chatterbox-nats/chatterbox-nats-impl/src/main/java/org/apache/tomee/chatterbox/nats/adapter/NATSResourceAdapter.java
@@ -45,10 +45,13 @@ import java.lang.reflect.Proxy;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.lang.IllegalStateException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 @Connector(description = "Sample Resource Adapter", displayName = "Sample Resource Adapter", eisType = "Sample Resource Adapter", version = "1.0")
 public class NATSResourceAdapter implements ResourceAdapter {
-    final Map<NATSActivationSpec, EndpointTarget> targets = new ConcurrentHashMap<NATSActivationSpec, EndpointTarget>();
+    private static final Logger LOGGER = Logger.getLogger(NATSResourceAdapter.class.getName());
+    private final Map<NATSActivationSpec, EndpointTarget> targets = new ConcurrentHashMap<NATSActivationSpec, EndpointTarget>();
 
     private static final Method ONMESSAGE;
 
@@ -63,6 +66,12 @@ public class NATSResourceAdapter implements ResourceAdapter {
     @ConfigProperty
     private String baseAddress;
 
+    @ConfigProperty
+    private String clientId;
+
+    @ConfigProperty
+    private String clusterId;
+
     private WorkManager workManager;
     private StreamingConnectionFactory cf;
     private StreamingConnection connection;
@@ -73,12 +82,11 @@ public class NATSResourceAdapter implements ResourceAdapter {
         try {
             cf = new
                     StreamingConnectionFactory(new Options.Builder().natsUrl(baseAddress)
-                    .clusterId("yourclientid").clientId("anythingyoulike").build());
+                    .clusterId(clusterId).clientId(clientId).build());
 
             connection = cf.createConnection();
         } catch (Throwable t) {
-            // TODO: log this
-            t.printStackTrace();
+            LOGGER.log(Level.SEVERE, "Error starting connection to NATS server", t);
         }
     }
 
@@ -86,7 +94,7 @@ public class NATSResourceAdapter implements ResourceAdapter {
         try {
             connection.close();
         } catch (Throwable t) {
-            // TODO: log this
+            LOGGER.log(Level.SEVERE, "Error closing connection to NATS server", t);
         }
     }
 
@@ -136,7 +144,6 @@ public class NATSResourceAdapter implements ResourceAdapter {
     }
 
     public void publish(final String subject, final byte[] data) throws NATSException {
-        // publish a message
         try {
             connection.publish(subject, data);
         } catch (Exception e) {
@@ -178,7 +185,7 @@ public class NATSResourceAdapter implements ResourceAdapter {
                     messageEndpoint.afterDelivery();
                 }
             } catch (Throwable t) {
-                // TODO: log this
+                LOGGER.log(Level.SEVERE, "Error dispatching message from NATS to MDB endpoint", t);
             }
         }
 
@@ -196,7 +203,7 @@ public class NATSResourceAdapter implements ResourceAdapter {
                     subscription.close(true);
                 }
             } catch (IOException e) {
-                // TODO: log this
+                LOGGER.log(Level.SEVERE, "Error closing subscription to NATS subject", e);
             }
         }
     }
diff --git a/chatterbox-nats/chatterbox-nats-rar/src/main/rar/META-INF/ra.xml b/chatterbox-nats/chatterbox-nats-rar/src/main/rar/META-INF/ra.xml
index 3d4ed8e..790de56 100644
--- a/chatterbox-nats/chatterbox-nats-rar/src/main/rar/META-INF/ra.xml
+++ b/chatterbox-nats/chatterbox-nats-rar/src/main/rar/META-INF/ra.xml
@@ -35,6 +35,14 @@
       <config-property-name>baseAddress</config-property-name>
       <config-property-type>String</config-property-type>
     </config-property>
+    <config-property>
+      <config-property-name>clientId</config-property-name>
+      <config-property-type>String</config-property-type>
+    </config-property>
+    <config-property>
+      <config-property-name>clusterId</config-property-name>
+      <config-property-type>String</config-property-type>
+    </config-property>
     <outbound-resourceadapter>
       <connection-definition>
         <managedconnectionfactory-class>org.apache.tomee.chatterbox.nats.adapter.out.NATSManagedConnectionFactory
diff --git a/chatterbox-nats/docker-compose.yml b/chatterbox-nats/docker-compose.yml
index f1dab81..7d94584 100644
--- a/chatterbox-nats/docker-compose.yml
+++ b/chatterbox-nats/docker-compose.yml
@@ -16,7 +16,7 @@ services:
        - '2'
        - '-SD'
        - '-cid'
-       - 'yourclientid'
+       - 'mycluster'
      environment:
        TZ: Europe/London
        LANG: en_GB.UTF-8