Author: Konstantin Bokarius kon@fanout.io
A Java convenience library for publishing messages using the EPCP protocol.
java-pubcontrol is offered under the MIT license. See the LICENSE file.
java-pubcontrol is compatible with JDK 6 and above.
Maven:
<dependency>
<groupId>org.fanout</groupId>
<artifactId>pubcontrol</artifactId>
<version>1.0.10</version>
</dependency>Note that on some operating systems Java may require you to add the root CA certificate of the publishing server to the key store. This is particularly the case with OSX. Follow the steps outlined in this article to address the issue: http://nodsw.com/blog/leeland/2006/12/06-no-more-unable-find-valid-certification-path-requested-target
Also, if using Java 6 you may run into SNI issues. If this occurs we recommend HTTP-only publishing or upgrading to Java 7 or above.
importorg.fanout.pubcontrol.*;
importjava.util.*;
publicclassPubControlExample {
privatestaticclassHttpResponseFormatimplementsFormat {
privateStringbody;
publicHttpResponseFormat(Stringbody) {
this.body = body;
}
publicStringname() {
return"http-response";
}
publicMap<String, Object> export() {
Map<String, Object> export = newHashMap<String, Object>();
export.put("body", body);
returnexport;
}
}
privatestaticclassCallbackimplementsPublishCallback {
publicvoidcompleted(booleanresult, Stringmessage) {
if (result)
System.out.println("Publish successful");
elseSystem.out.println("Publish failed with message: " + message);
}
}
publicstaticvoidmain(String[] args) {
// PubControl can be initialized with or without an endpoint configuration.// Each endpoint can include optional JWT authentication info.// Multiple endpoints can be included in a single configuration.// Initialize PubControl with a single endpoint:List<Map<String, Object>> config = newArrayList<Map<String, Object>>();
Map<String, Object> entry = newHashMap<String, Object>();
entry.put("uri", "https://api.fastly.com/service/<service-id>");
// The API token needs to have purge permissionentry.put("key", "<fastly-api-token>");
config.add(entry);
PubControlpub = newPubControl(config);
// Add new endpoints by applying an endpoint configuration:config = newArrayList<Map<String, Object>>();
HashMap<String, Object> entry1 = newHashMap<String, Object>();
entry1.put("uri", "<myendpoint_uri_1>");
config.add(entry1);
HashMap<String, Object> entry2 = newHashMap<String, Object>();
entry2.put("uri", "<myendpoint_uri_2>");
config.add(entry2);
pub.applyConfig(config);
// Remove all configured endpoints:pub.removeAllClients();
// Explicitly add an endpoint as a PubControlClient instance:PubControlClientpubClient = newPubControlClient("<myendpoint_uri>");
// Optionally set JWT auth: pubClient.setAuthJwt(<claims>, '<key>')// Optionally set basic auth: pubClient.setAuthBasic('<user>', '<password>')pub.addClient(pubClient);
// Publish across all configured endpoints:List<String> channels = newArrayList<String>();
channels.add("test_channel");
List<Format> formats = newArrayList<Format>();
formats.add(newHttpResponseFormat("Test publish!"));
try {
pub.publish(channels, newItem(formats, null, null));
} catch (PublishFailedExceptionexception) {
System.out.println(exception.getMessage());
exception.printStackTrace();
}
pub.publishAsync(channels, newItem(formats, null, null), newCallback());
// Wait for all async publish calls to complete:pub.finish();
}
}