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 update on start up 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.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngineBuilder;
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 java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.LoggerFactory;
public class UpdateOnStartUp 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 {
public Example(boolean printOutput) {
super(printOutput);
}
public void run(String dataFile, String licenseKey) throws Exception {
println("Using data file at " + dataFile);
Date initialPublishedDate;
HttpClient httpClient = new HttpClientDefault();
try(DataUpdateService tmpDataUpdateService = new DataUpdateServiceDefault(
LoggerFactory.getLogger(DataUpdateServiceDefault.class.getName()),
httpClient)) {
try(DeviceDetectionHashEngine deviceDetectionHashEngine = new DeviceDetectionHashEngineBuilder(
LoggerFactory.getILoggerFactory(), tmpDataUpdateService).build(dataFile, true)) {
initialPublishedDate = deviceDetectionHashEngine.getDataFilePublishedDate();
}
}
println("Data file published date: " + initialPublishedDate.toString());
println("Creating pipeline and updating device data");
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
while(!executor.isShutdown()){
print(".");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
});
DataUpdateService dataUpdateService = new DataUpdateServiceDefault(
LoggerFactory.getLogger(DataUpdateServiceDefault.class.getName()),
httpClient);
try {
dataUpdateService.onUpdateComplete(new GetAutoUpdateStatus());
Pipeline pipeline = new DeviceDetectionPipelineBuilder(
LoggerFactory.getILoggerFactory(), httpClient, dataUpdateService)
.useOnPremise(dataFile, true)
.setDataUpdateLicenseKey(licenseKey)
.setAutoUpdate(true)
.setDataFileSystemWatcher(true)
.setDataUpdateOnStartup(true)
.build();
executor.shutdownNow();
Date updatedPublishedDate = pipeline
.getElement(DeviceDetectionHashEngine.class)
.getDataFilePublishedDate();
if(initialPublishedDate.equals(updatedPublishedDate))
{
println("There was no update available at this time.");
}
println("Data file published date: " + updatedPublishedDate.toString());
System.in.read();
}
finally {
dataUpdateService.close();
}
}
class GetAutoUpdateStatus implements OnUpdateComplete {
@Override
public void call(Object o, DataUpdateCompleteArgs duca) {
System.out.println("Update completed. Status " + duca.getStatus());
}
}
}
}