Subscribe to Event Hub in Java

To programmatically subscribe to Event Hub or Event Analytics with your own Java class:

Write the Java code, get the libraries from the Event Hub and Event Analytics applications in the Infor Grid, download the simple SLF4J logger, compile, and run. I used this code for my demo of picking lists in Google Glass; I would not use this in a production environment.

DISCLAIMER: INFOR DOES NOT SUPPORT USAGE OF CUSTOM SUBSCRIBER OR PUBLISHER CLIENTS IN EXTERNAL APPLICATIONS. ONLY INFOR PROCESS AUTOMATION (IPA) AND M3 ENTERPRISE COLLABORATOR (MEC) ARE ALLOWED TO CONNECT TO THE EVENT HUB. USE AT YOUR OWN RISK. MAY VOID YOUR WARRANTY.

/*
javac -cp eventhub-common-2.0.20.jar;eventhub-subscriber-2.0.20.jar;slf4j-api-1.6.2.jar TestEventSubscriber.java
java -cp eventhub-common-2.0.20.jar;eventhub-subscriber-2.0.20.jar;slf4j-api-1.6.2.jar;hornetq-core-client-2.3.0.CR1.jar;hornetq-commons-2.3.0.CR1.jar;slf4j-simple-1.7.12.jar;jboss-logging-3.1.0.GA.jar;jboss-logmanager-1.2.2.GA.jar;netty-3.6.2.Final.jar;. TestEventSubscriber
*/

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lawson.eventhub.ElementData;
import com.lawson.eventhub.EventData;
import com.lawson.eventhub.Subscription;
import com.lawson.eventhub.Subscription.Builder;
import com.lawson.eventhub.subscriber.EventReceiver;
import com.lawson.eventhub.subscriber.Subscriber;
import com.lawson.eventhub.subscriber.SubscriberException;

public class TestEventSubscriber {

    static String name = "MyTest";
    static String hostName = "host";
    static int portNumber = 22110;
    static String subscription = "M3:OCUSMA:U"; // or EventAnalytics:something
    static Subscriber subscriber;
    static Logger log = LoggerFactory.getLogger(TestEventSubscriber.class);

    public static void main(String[] args) throws Exception {
        register();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            /* stop when CTRL+C */
            public void run() {
                try {
                    unregister();
                } catch (Exception ex) {
                    log.error("CTRL+C", ex);
                }
            }
        });
    }

    public static void register() throws Exception {
        log.info("Registering...");
        // create subscriber
        Map<String, String> params = new HashMap<String, String>();
        params.put("subscriber-name", name);
        params.put("subscriber-persist", "false");
        params.put("subscriber-server-address", hostName);
        params.put("subscriber-server-port", Integer.toString(portNumber));
        subscriber = new Subscriber(params);
        // add subscription
        Subscription sub = Subscription.newBuilder(subscription).build();
        subscriber.add(sub);
        // register receiver
        EventReceiver receiver = new TestEventReceiver();
        subscriber.register(receiver);
        // probe
        log.info(
            "isActive: " + subscriber.isActive() +
            ", isConnected: " + subscriber.isConnected() +
            ", isRegistered: " + subscriber.isRegistered() +
            ", isFailed: " + subscriber.isFailed());
    }

    public static void unregister() throws Exception {
        log.info("Unregistering...");
        subscriber.unregister();
    }
}

class TestEventReceiver implements EventReceiver {
    static Logger log = LoggerFactory.getLogger(TestEventReceiver.class);
    public boolean receiveEvent(EventData event) throws SubscriberException {
        log.info(
            "Subscription: " + event.getSubscription() +
            ", Publisher: " + event.getPublisher() +
            ", DocumentName: " + event.getDocumentName() +
            ", Operation: " + event.getOperation().character() +
            ", TrackingId: " + event.getTrackingId() +
            ", SentTimestamp: " + new Date(event.getSentTimestamp()).toString() +
            ", CONO: " + event.getElementValue("CONO"));
        for (ElementData element : event.getElements()) {
            log.info(
                "Name: " + element.getName() +
                ", OldValue: " + element.getOldValue() +
                ", Value: " + element.getValue());
        }
        return true;
    }
}

b
It will persist a file data/subscriber/delivered/MyTest.rcv that contains a bunch of unique ids and that you can deserialize with a ConcurrentHashMap.
Thank you.

Published by

thibaudatwork

ex- M3 Technical Consultant

20 thoughts on “Subscribe to Event Hub in Java”

  1. UPDATE: Subscriptions are un-aunthenticated and un-encrypted by default (yikes!). Event Hub can be configured with TLS to authenticate the client and encrypt traffic. To be tested.

    Like

  2. Hello Thibaud. I have one question about CMS045 settings. I set up CMS045 with the CREATE event subscription (on OASITN file, item/assortment) and it’s working fine : a record is added into CUGEX1 any time i add a record in OIS072. Now if i delete the same record in OIS072, i go through the rule in EventAnalytics (i see my System.out.println in the log) but it does not delete the record in CUGEX1. Is it working as designed ? You can create, update but not delete ? Thank you !

    Like

    1. Hi Max. I don’t know, I haven’t used CMS045 much. I didn’t even know it added records in CUGEX1. When you delete a record from OIS072, does it add a new record in CUGEX1 with operation=Delete or something? I would expect it to always append records. I would be surprised if it deleted records.

      Like

      1. The creation of records in CUGEX1 is only due to the “event fields mapping” related option i set up. I think you are right, it can only create/update records in other tables, even with a DELETE operation. You could mark the record as “deleted” in this extension table file (put a status 90 in a free field for example) but i will never physically delete the record. Yeah, except a SQL statement in the rule! Thank you for your answer ; )

        Like

        1. Hi maxric,

          I’m working with eventhub and CMS045 event field mapping and my problem is that two or more process are subscribed to MITBAL update and will insert to CUGEX1. The second rule is having problem with duplicate key and I was planning on moving to CUGEX3. I was wondering though if I can create a new entry in CMS045 wherein instead of MITBAL event name I’ll use MITBAL1. Because event field mapping this time will be CUGEX3 and not CUGEX1. I was wondering if this is possible. Or should it always be the name of the table?

          Liked by 1 person

          1. Hi Doms,
            Sorry i just see your comment, it’s too late i guess ! You can add a subscription in CMS045 on event MITBAL1 (publisher EventAnalytics), correct. And this event MITBAL1 should be the one fired by EventAnalytcis (in the smart rule). In EventAnalytics, create a rule to subscribe to M3, mode CREATE on file MITBAL (add your different filters if needed) and fire event MITBAL1 in the “then” clause of the smart rule. Hope it helps.

            Like

            1. Oh, good to know. Although we opted to use MEC and CUSEXTMI instead and we just manipulated the keys so that we can insert multiple MITBAL rows in CUGEX1.

              Like

  3. Super article. Is that possible to test the all values ​​with all old values ​​to trigger only the real modifications.

    thank you for your response, best regards

    Like

  4. Thank you for your response. In fact I want to test the set of values ​​with the old values ​​in a rule, I do not arrive elementdata does not even after import. Maybe there is a property or method of HubEvent or something else. thank you for your response, best regards

    Like

  5. Thanks , I have gone well till compilation. I am trying to run it from local C:\ drive , its trying to find .rcv file and saying as file not found .
    Can you help on from where should we should run this class file ?

    Like

Leave a comment