Reviewing the PropertyTracker Sample App and APIs

PropertyTracker is a sample Windows application included in your Thriftly Developer install. PropertyTracker keeps track of a user’s purchases (including products purchased, the locations where purchases took place, purchase prices, etc.) by connecting to a MariaDB database.

PropertyTracker also contains several example Thriftly APIs, which you can use as a model when creating APIs of your own. We suggest you thoroughly review the PropertyTracker app, and the Thriftly-related code contained within, to learn how Thriftly APIs are created and defined within an application.

Reviewing the Sample Application

Note

Throughout the rest of this section, we’ll be using Eclipse for Java to import, review, and manage our sample application. We highly recomend installing and using Eclipse Java to review our sample application, as it simplifies the process.

To review the PropertyTracker application:

  1. Open Eclipse Java and select File > Import. The Import window appears.

  2. From the Select an import wizard: field, select General > Existing Projects into Workspace. Then, select the Next button.

  3. Select the Browse button located to the right of the Select root directory: field. Browse to <User>\Documents\Thriftly Developer\Java\PropertyTracker. Select OK.

  4. The PropertyTracker folder appears as the project’s root directory. Select Finish to import the project.

  5. The PropertyTracker project appears in the Package Explorer sidebar. Expand the project, then expand the src and (default package) sub-folders. From there, select and open the project’s Main.java file.

The PropertyTracker project in the Package Explorer sidebar
  1. The application’s Main.java file appears in your primary Eclipse Java workspace. Examine the Main.java file and note how the application:

    • Imports the thriftly.java.ThriftlyServer package.

    The import thriftly.java.ThriftlyServer directive
    • Instantiates a Thriftly Server object that includes three services.

    The ThriftlyServer object and Thriftly services
    • Starts the Thriftly Server application.

    The thriftly.StartServer directive
  2. Before we move on, you’ll also want to note the Thriftly.Java.jar library referenced within the PropertyTracker application. Referencing this library allows PropertyTracker to include a variety of Thriftly-related directives and metadata (including those we pointed out above).

The Thriftly.Java.jar library

As you’ve seen, PropertyTracker includes three services, LocationService, ItemService, and ProductDirectoryService.

The PropertyTracker services

Each of these services comprises a separate Thriftly API, allowing outside users and applications to make API calls to functions contained within the PropertyTracker application. Now that you’ve seen how these services are included in the application’s Main.java file, let’s examine one of the services themselves.

Reviewing an Example Service

Let’s take a look at the LocationService service:

  1. From the Solution Explorer sidebar, expand the services sub-folder. Then, select and double-click LocationService.java.

  2. LocationService.java opens in the main Eclipse Java workspace. Examine the service, taking particular note of the following:

    • The import thriftly.java.PublishedAttribute directive. (You may have to expand the file’s list of import directives, by selecting the [+] button in line 3, to see this directive.) This directive allows a service to reference the @PublishedAttribute attribute, which we’ll explain next.

    The import thriftly.java.PublishedAttribute directive
    • The @PublishedAttribute. This attribute tells the Thriftly Server which functions users can call when they access your API. In LocationService, you’ll see the @PublishedAttribute above the getLocation, searchLocations, addEditLocation, and deleteLocation functions. Users can access those functions when they interact with our LocationService Thriftly API. Note that if our service included other functions that did not include the @PublishedAttribute, users would not be able to call those functions when making requests of our API.

    The PublishedAttribute attribute

The service itself contains a lot more code, but the two pieces we mentioned above are the pieces that define this service as a Thriftly API. Otherwise, LocationService is simply a normal Java service, built as you’d build any other Java service you’d include within an application.

If you open and examine the ItemService and ProductDirectoryService services, you’ll find they’re constructed the same way. Now that you understand how the services that become Thriftly APIs are constructed, let’s run the PropertyTracker application and examine exactly how application services translate into Thriftly APIs.

Running the Sample Application and APIs

Let’s compile and run PropertyTracker, so we can access our services as Thriftly APIs.

  1. Before you can run the application, you’ll have to adjust PropertyTracker’s build path so it includes the imported Thriftly Java libraries. To do so:

    1. Right-click the Property Tracker project in the Package Explorer sidebar.

    2. Select Build Path > Configure Build Path. The Properties for PropertyTracker window appears.

    3. On the Source tab, expand the PropertyTracker/src folder and select Native library location: (None). Then, select the Edit button. The Native Library Folder Configuration window appears.

    4. Select the Workspace button, then select Property Tracker > externalJars. Then, select OK, OK, Apply and Close. PropertyTracker’s now configured to include the Thriftly Java libraries on its build path.

  2. From the Eclipse toolbar, select the Run button.

Note

If you ignored the Installation Requirements, and you’re using the 64-bit version of Java Runtime Environment (JRE) instead of the 32-bit version, you’ll receive an error when you attempt to run PropertyTracker. To download the 32-bit version of JRE, click here. To learn how to manually switch a project to reference a different version of Java (in this case, the 32-bit version), watch this video.

When you run the application, the Thriftly Developer window appears to allow you to start configuring and testing your APIs.

To test out the PropertyTracker APIs using Thriftly’s API testing interface, head over to the Testing Your Thriftly API section. After you’ve finished playing around with the PropertyTracker APIs, you’ll be ready to create some basic Thriftly APIs of your own, using the instructions in the next section.