This Example shows how to configure the usage sharing feature of
the Pipeline API. Usage sharing is a feature that allows you to share details
of the requests you are processing with 51Degrees.
Usage sharing works by adding a 'ShareUsageElement' to the pipeline.
This element will collect evidence values as they pass through,
periodically sending them to 51Degrees for processing.
If you want to know more about usage sharing, and why you should
consider using it, see the usage sharing
feature page.
Usage sharing is enabled by default if using a pipeline builder that is derived from FiftyOnePipelineBuilder. For instance, the DeviceDetectionPipelineBuilder. In this example, we show how to add a ShareUsageElement to a Pipeline using configuration.
As with all ElementBuilders, this can also be handled in code, using the ShareUsageBuilder. The commented section in the example demonstrates this.
The 51d.xml file contains all the configuration options. These are all optional, so each can be omitted if the default for that option is sufficient:
1 <?
xml version=
"1.0" encoding=
"utf-8" ?>
28 <
SharePercentage>0.1</
SharePercentage>
29 <
ShareUsageUrl>
https://
devices-v4.51degrees.com/
new.ashx</
ShareUsageUrl>
30 <
MinimumEntriesPerMessage>50</
MinimumEntriesPerMessage>
31 <
RepeatEvidenceIntervalMinutes>10</
RepeatEvidenceIntervalMinutes>
32 <
TrackSession>
false</
TrackSession>
33 <
SessionCookieName></
SessionCookieName>
34 <
IncludedQueryStringParameters>
parameter1,
parameter2</
IncludedQueryStringParameters>
35 <
BlockedHttpHeader>
mySecretHeader</
BlockedHttpHeader>
36 <
IgnoreFlowDataEvidenceFilter>
evidenceKey:evidenceValue</
IgnoreFlowDataEvidenceFilter>
37 <
AddTimeout>5</
AddTimeout>
38 <
TakeTimeout>100</
TakeTimeout>
40 <
BuilderName>
ShareUsage</
BuilderName>
For details of what each setting does, see the reference documentation for the share usage builder
This example is available in full on GitHub.
Expected output:
Constructing pipeline from configuration file.
Pipeline created with share usage element. Evidence processed
with this pipeline will now be shared with 51Degrees using the
specified configuration.
package pipeline.developerexamples.usagesharing;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.core.flowelements.PipelineBuilder;
import fiftyone.pipeline.engines.fiftyone.flowelements.ShareUsageElement;
import fiftyone.pipeline.engines.fiftyone.flowelements.ShareUsageBuilder;
import fiftyone.pipeline.engines.services.HttpClient;
import fiftyone.pipeline.engines.services.HttpClientDefault;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;
import java.io.File;
public class Main {
private static final ILoggerFactory loggerFactory =
LoggerFactory.getILoggerFactory();
private static final HttpClient http = new HttpClientDefault();
public static class Example {
public void run() throws Exception {
System.out.println("Constructing pipeline from configuration file.");
System.out.println();
File file = new File(getClass().getClassLoader().getResource("51d.xml").getFile());
JAXBContext jaxbContext = JAXBContext.newInstance(PipelineOptions.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
PipelineOptions options = (PipelineOptions)unmarshaller.unmarshal(file);
Pipeline pipeline = new PipelineBuilder()
.addService(http)
.buildFromConfiguration(options);
System.out.println(
"Pipeline created with share usage element. Evidence processed " +
"with this pipeline will now be periodically shared with " +
"51Degrees using the specified configuration.");
}
}
public static void main(String[] args) throws Exception {
new Example().run();
}
}