Connect Java Library

connect-client is a Java library for using the Urban Airship Connect API.

Resources

Installation

Maven Installation

<!-- Urban Airship Library Dependency-->
<dependency>
    <groupId>com.urbanairship</groupId>
    <artifactId>connect-client</artifactId>
    <version>VERSION</version>
</dependency>

You can add the library using Maven by adding the following lines to your pom.xml:

 

Example of what your settings.xml may look like after adding bintray.

<?xml version="1.0" encoding="UTF-8" ?>
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd'
          xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>

    <profiles>
        <profile>
            <repositories>
                <repository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>bintray-urbanairship-java-client</id>
                    <name>bintray</name>
                    <url>https://urbanairship.bintray.com/java-client</url>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <id>bintray-urbanairship-java-client</id>
                    <name>bintray-plugins</name>
                    <url>https://urbanairship.bintray.com/java-client</url>
                </pluginRepository>
            </pluginRepositories>
            <id>bintray</id>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>bintray</activeProfile>
    </activeProfiles>
</settings>

Then configure the maven settings.xml file to use bintray.

Configuration

The client library provides all the components you need to consume a mobile event stream.

Note that Connect requests with this client may experience SSL handshake failures unless using the Java Cryptography Extension (JCE) Unlimited Strength package cipher suite.

If you encounter a generic connection failure java.lang.RuntimeException, the max strength encryption policy might be the culprit, and you should ensure this JCE Unlimited Strength package is installed on your system.

You can find the JCE Unlimited Strength package at the following locations. Choose the one that corresponds to your JRE version.

These files are not required for JRE 9 or for JRE 8u151 or newer.

Getting Started

Setting up the Stream

Example stream configuration

  Creds creds = Creds.newBuilder()
          .setAppKey("key")
          .setToken("token")
          .build();

  StreamQueryDescriptor descriptor = StreamQueryDescriptor.newBuilder()
          .setCreds(creds)
          .build();

To set up the stream to get ready for consumption first you set the Creds then create a StreamQueryDescriptor and use the descriptor to create a Stream.

Consume the Stream

Example stream that disconnects after 60 seconds.

  final Stream stream = new Stream(descriptor, Optional.<StartPosition>absent());

  final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

  Runnable stopConsuming = new Runnable() {
      public void run() {
          try {
              stream.close();
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              scheduledExecutorService.shutdown();
          }
      }
  };

  scheduledExecutorService.schedule(stopConsuming, 60, TimeUnit.SECONDS);

  while (stream.hasNext()) {
      String event = stream.next();
      System.out.println("Event: " + event);
  }

After the steam is configured the stream can be consumed.

Filtering

Various filter and stream customization example.

  Optional<StartPosition> startPosition = Optional.fromNullable(StartPosition.relative(StartPosition.RelativePosition.EARLIEST));

  DeviceFilter device1 = new DeviceFilter(DeviceFilterType.ANDROID_CHANNEL, "152d00c3-c49c-4172-88ce-539c511cf346");
  DeviceFilter device2 = new DeviceFilter(DeviceFilterType.IOS_CHANNEL, "67fa2bad-9e83-4259-b925-bc08c184f72e");
  DeviceFilter device3 = new DeviceFilter(DeviceFilterType.NAMED_USER_ID, "cool_user");

  NotificationFilter notification = new NotificationFilter(NotificationFilter.Type.GROUP_ID, "58179035-dd1f-4b04-b023-5035c6335786");

  Filter filter = Filter.newBuilder()
          .setLatency(20000000)
          .addDevices(device1, device2, device3)
          .addDeviceTypes(DeviceType.ANDROID, DeviceType.IOS)
          .addNotifications(notification)
          .addEventTypes("OPEN")
          .build();

  Subset subset = Subset.createSampleSubset(0.3f);

  StreamQueryDescriptor descriptor = StreamQueryDescriptor.newBuilder()
          .setCreds(creds)
          .addFilters(filter)
          .setSubset(subset)
          .build();

  final Stream stream = new Stream(descriptor, startPosition);

Filter, Subset, and DeviceFilter can also be applied to a stream to retrieve whatever information that is wanted.