/* * * Copyright (c) 1999 - 2011 my-Channels Ltd * Copyright (c) 2012 - 2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors. * * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG. * */ package com.pcbsys.nirvana.apps; import com.pcbsys.nirvana.client.*; public class DataGroupDeltaDelivery extends nSampleApp implements nDataStreamListener { /** * Writes to a nirvana data group using registered events. */ private static DataGroupDeltaDelivery mySelf = null; /** * This method demonstrates the Nirvana API calls necessary to write to a * data group. Each time a message is written, only those properties which * have changed will be sent to the server. It is called after all command * line arguments have been received and validated. * * @param realmDetails a String[] containing the possible RNAME values * @param count the number of iterations */ private void doit(String[] realmDetails, int count) throws nBaseClientException { mySelf.constructSession(realmDetails, this); try { //Create a data group nDataGroup aud_dg = mySession.createDataGroup("AUDGroup"); nDataGroup gbp_dg = mySession.createDataGroup("GBPGroup"); nDataGroup eur_dg = mySession.createDataGroup("EURGroup"); //add our stream to the groups so that we receive callbacks when an event is received aud_dg.add(myStream); gbp_dg.add(myStream); eur_dg.add(myStream); System.out.println("Creating and registering events"); nRegisteredEvent aud = null; nRegisteredEvent gbp = null; nRegisteredEvent eur = null; try { aud = aud_dg.createRegisteredEvent(); nEventProperties props = aud.getProperties(); props.put("bid", 0.8999); props.put("offer", 0.9999); props.put("description", "Australian Dollar"); props.put("time", (new java.util.Date().toString())); gbp = gbp_dg.createRegisteredEvent(); props = gbp.getProperties(); props.put("bid", 0.8999); props.put("offer", 0.9999); props.put("description", "English Pound"); props.put("time", (new java.util.Date().toString())); eur = eur_dg.createRegisteredEvent(); props = eur.getProperties(); props.put("bid", 0.8999); props.put("offer", 0.9999); props.put("description", "Euro"); props.put("time", (new java.util.Date().toString())); // The events are now registered with the data group so commit them // to the server // True is passed as a parameter to ensure we reset the event on the server try { aud.commitChanges(true); gbp.commitChanges(true); eur.commitChanges(true); } catch (nTransactionException e) { System.out.println("Could not commit transaction."); e.printStackTrace(); } catch (nChannelNotFoundException e) { System.out.println("Channel could not be found."); e.printStackTrace(); } } catch (nIllegalArgumentException e1) { e1.printStackTrace(); } System.out.println("All events are now registered and committed to the server"); // Loop as many times as the number of messages we want to publish for (int i = 0; i < count; i++) { try { // Both name and bid will be sent to the server as we readd them on each iteration. // The server will see that these properties have not changed so will not send the // events to the subscriptions aud.getProperties().put("bid", 0.7999); aud.getProperties().put("name", "James"); aud.commitChanges(); Thread.sleep(2000); // On the first iteration the consumer will receive all properties of the event because // each value has changed. On subsequent iterations, the bid and offer do not change // so the consumer will only receive the 'time' property gbp.getProperties().put("offer", 1.567); gbp.getProperties().put("bid", 1.67888); gbp.getProperties().put("time", (new java.util.Date().toString())); gbp.commitChanges(); Thread.sleep(2000); //Because 'true' is passed to commitChanges, the current event registered on the server //will be overwritten with this event. Therefore the consumer will receive all properties //on each iteration. eur.getProperties().put("offer", 1.567); eur.getProperties().put("bid", 1.67888); eur.getProperties().put("time", (new java.util.Date().toString())); eur.getProperties().put("description", "Euro dollar / USD"); eur.commitChanges(true);//Overwrites the current event on the server Thread.sleep(2000); } catch (nSessionNotConnectedException ex) { while (!mySession.isConnected()) { System.out.println("Disconnected from Nirvana, Sleeping for 1 second..."); try { Thread.sleep(1000); } catch (Exception e) { } } } catch (nBaseClientException ex) { System.out.println("Exception : " + ex.toString()); throw ex; } catch (InterruptedException e) { System.out.println("Thread Interrupted"); e.printStackTrace(); } } } catch (nSessionPausedException ps) { System.out.println("Session has been paused, please resume the session"); System.exit(1); } catch (nSecurityException se) { System.out.println("Insufficient permissions for the requested operation."); System.out.println("Please check the ACL settings on the server."); System.exit(1); } catch (nSessionNotConnectedException snce) { System.out.println("The session object used is not physically connected to the Nirvana realm."); System.out.println("Please ensure the realm is up and check your RNAME value."); System.exit(1); } catch (nUnexpectedResponseException ure) { System.out.println("The Nirvana REALM has returned an unexpected response."); System.out.println("Please ensure the Nirvana REALM and client API used are compatible."); System.exit(1); } catch (nRequestTimedOutException rtoe) { System.out.println("The requested operation has timed out waiting for a response from the REALM."); System.out.println("If this is a very busy REALM ask your administrator to increase the client timeout values."); System.exit(1); } // Close the session we opened try { nSessionFactory.close(mySession); } catch (Exception ex) { } // Close any other sessions so that we can exit nSessionFactory.shutdown(); } public void onMessage(nConsumeEvent evt) { System.out.println("Group : " + evt.getDataGroupName()); System.out.println("Event id : " + evt.getEventID()); System.out.println("Event tag : " + evt.getEventTag()); System.out.println("Is Delta : " + evt.getAttributes().isDelta()); // Print the message data if (evt.hasAttributes()) { displayEventAttributes(evt.getAttributes()); } nEventProperties prop = evt.getProperties(); if (prop != null) { displayEventProperties(prop); } } protected void processArgs(String[] args) { // requires RNAME String RNAME = ""; if (System.getProperty("RNAME") == null) { Usage(); System.exit(1); } RNAME = System.getProperty("RNAME"); int count = 10; if (args.length > 0) { count = Integer.parseInt(args[0]); } // // Run the sample app // try { mySelf.doit(parseRealmProperties(RNAME), count); } catch (nBaseClientException e) { e.printStackTrace(); } } public static void main(String[] args) { // Create an instance for this class mySelf = new DataGroupDeltaDelivery(); // Process command line arguments mySelf.processArgs(args); } /** * Prints the usage message for this class */ private static void Usage() { System.out.println("Usage ...\n"); System.out.println("DataGroupDeltaDelivery [count] \n"); System.out.println("[Optional Arguments] \n"); System.out.println("[count] - the number of times to commit the registered events"); } }