51Degrees Pipeline Java  4.3Newer Version 4.4

51Degrees Pipeline for Java


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" ?>
2 <PipelineOptions>
3  <Elements>
4  <Element>
5  <BuildParameters>
6  <SharePercentage>0.1</SharePercentage>
7  <ShareUsageUrl>https://devices-v4.51degrees.com/new.ashx</ShareUsageUrl>
8  <MinimumEntriesPerMessage>50</MinimumEntriesPerMessage>
9  <RepeatEvidenceIntervalMinutes>10</RepeatEvidenceIntervalMinutes>
10  <TrackSession>false</TrackSession>
11  <SessionCookieName></SessionCookieName>
12  <IncludedQueryStringParameters>parameter1,parameter2</IncludedQueryStringParameters>
13  <BlockedHttpHeader>mySecretHeader</BlockedHttpHeader>
14  <IgnoreFlowDataEvidenceFilter>evidenceKey:evidenceValue</IgnoreFlowDataEvidenceFilter>
15  <AddTimeout>5</AddTimeout>
16  <TakeTimeout>100</TakeTimeout>
17  </BuildParameters>
18  <BuilderName>ShareUsage</BuilderName>
19  </Element>
20  </Elements>
21 </PipelineOptions>

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.
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
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 =
// The HTTP service that will be used to send usage
// data back to 51Degrees.
private static final HttpClient http = new HttpClientDefault();
public static class Example {
public void run() throws Exception {
System.out.println("Constructing pipeline from configuration file.");
// Create the configuration object from an XML file
File file = new File(getClass().getClassLoader().getResource("51d.xml").getFile());
JAXBContext jaxbContext = JAXBContext.newInstance(PipelineOptions.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Bind the configuration to a pipeline options instance
PipelineOptions options = (PipelineOptions)unmarshaller.unmarshal(file);
// Build a new Pipeline from the configuration.
Pipeline pipeline = new PipelineBuilder()
// Alternatively, the commented code below shows how to
// configure the ShareUsageElement in code, rather than
// using a configuration file.
//ShareUsageElement usageElement = new ShareUsageBuilder(loggerFactory, http)
// .setSharePercentage(0.01)
// .setMinimumEntriesPerMessage(2500)
// .build();
//Pipeline pipeline = new PipelineBuilder()
// .addFlowElement(usageElement)
// .build();
"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();