Automatic updates example for 51Degrees device detection ‘Hash’ engine
shows how to build a pipeline using the 51Degrees device detection pipeline builder
and demonstrates the file system watcher functionality.
The automatic data file updates
page explains the different options you can configure.
This example requires a subscription to 51Degrees Device Data, a subscription can be acquired
from the 51Degrees pricing page.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
package fiftyone.devicedetection.examples.hash.automaticupdates;
import fiftyone.devicedetection.DeviceDetectionPipelineBuilder;
import fiftyone.devicedetection.examples.ExampleBase;
import fiftyone.devicedetection.examples.ProgramBase;
import fiftyone.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngine;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.services.DataUpdateService;
import fiftyone.pipeline.engines.services.DataUpdateService.DataUpdateCompleteArgs;
import fiftyone.pipeline.engines.services.DataUpdateServiceDefault;
import fiftyone.pipeline.engines.services.HttpClient;
import fiftyone.pipeline.engines.services.HttpClientDefault;
import fiftyone.pipeline.engines.services.OnUpdateComplete;
import java.util.Date;
import org.slf4j.LoggerFactory;
public class DataFileSystemWatcher extends ProgramBase {
public static void main(String[] args) throws Exception {
String dataFile = args.length > 0 ? args[0] :
getDefaultFilePath("51Degrees-LiteV4.1.hash").getAbsolutePath();
String licenseKey = args.length > 1 ? args[1] :
"!!Your license key!!";
if(licenseKey.startsWith("!!")) {
System.out.println("You need a license key to run this example, "
+ "you can obtain one by subscribing to a 51Degrees bundle: "
+ "https://51degrees.com/pricing");
System.in.read();
return;
}
new Example(true).run(dataFile, licenseKey);
System.out.println("Complete. Press enter to exit.");
System.in.read();
}
public static class Example extends ExampleBase {
static Pipeline pipeline;
public Example(boolean printOutput) {
super(printOutput);
}
public void run(String dataFile, String licenseKey) throws Exception {
println("Using data file at " + dataFile);
HttpClient httpClient = new HttpClientDefault();
DataUpdateService dataUpdateService = new DataUpdateServiceDefault(
LoggerFactory.getLogger(DataUpdateServiceDefault.class.getName()),
httpClient);
try {
dataUpdateService.onUpdateComplete(new GetAutoUpdateStatus());
pipeline = new DeviceDetectionPipelineBuilder(
LoggerFactory.getILoggerFactory(), httpClient, dataUpdateService)
.useOnPremise(dataFile, true)
.setDataUpdateLicenseKey(licenseKey)
.setAutoUpdate(true)
.setDataFileSystemWatcher(true)
.setUpdatePollingInterval(30)
.setUpdateRandomisationMax(10)
.setDataUpdateOnStartup(false)
.build();
Date publishedDate = pipeline
.getElement(DeviceDetectionHashEngine.class)
.getDataFilePublishedDate();
println("Data file published date: " + publishedDate);
println("The pipeline has now been set up to watch for changes " +
"to the data file at the path shown above.");
println("Copy a new data file over the existing one to trigger " +
"this process.");
println("You can obtain a new file from https://51degrees.com/developers/downloads/enhanced-device-data?licenceKey=" + licenseKey);
println("Press a key to end the program.");
System.in.read();
}
finally {
dataUpdateService.close();
}
}
public static class GetAutoUpdateStatus implements OnUpdateComplete {
@Override
public void call(Object o, DataUpdateCompleteArgs duca) {
if(pipeline != null) {
Date publishedDate = pipeline
.getElement(DeviceDetectionHashEngine.class)
.getDataFilePublishedDate();
System.out.println("Data file published date: " + publishedDate.toString());
}
System.out.println("Update completed. Status " + duca.getStatus());
}
}
}
}