public class Example {
public static void main(String[] args) {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(Parameters.NAME, "MyPublisher");
// If we are running outside Grid these two parameters must be supplied
// or the initialization below will throw an IllegalArgumentException.
// If running from within a Grid context the these parameters are resolved
// automatically and hence should be omitted.
parameters.put(Parameters.SERVER_HOST_ADDRESS, "eventhub.example.org");
parameters.put(Parameters.SERVER_PORT, "5445");
// Before using a publisher to publish events to the EventHub
// it must be initialized by calling Publisher.initInstance(name).
Publisher publisher;
try {
publisher = Publisher.initInstance(parameters);
} catch (PublisherException e) {
throw new RuntimeException(e);
}
EventData event = new EventData("MyDocument", EventOperation.CREATE);
event.addElement("postedByThread", Thread.currentThread().getName());
publisher.postEventWait(event);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// Once a publisher instance is initialized with a call to
// Publisher.initInstance(name) a reference to a thread safe
// instance is obtained from anywhere in the JVM by
// calling Publisher.getInstance(name).
Publisher publisher = Publisher.getInstance("MyPublisher");
EventData event = new EventData("MyDocument", EventOperation.CREATE);
event.addElement("postedByThread", Thread.currentThread().getName());
publisher.postEventWait(event);
}
});
try {
thread.start();
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// A publisher instance should ALWAYS be closed before exiting the publishing
// application. This will ensure that any in-flight events either will be
// committed in EventHub our persistently journaled locally and replayed later.
try {
publisher.close();
} catch (PublisherException e) {
throw new RuntimeException(e);
}
}
}