Skip to content

Commit

Permalink
Use fixed thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
dheid committed Nov 3, 2023
1 parent 99de7cb commit 7e410ea
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 281 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ info
# Package Files #
**/target/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
# virtual machine crash logs, see https://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDEA files
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Please note we have a code of conduct, please follow it in all your interactions
2. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
Pull Request would represent. The versioning scheme we use is [SemVer](https://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you.

Expand Down Expand Up @@ -86,8 +86,8 @@ members of the project's leadership.
### Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
available at [https://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[homepage]: https://contributor-covenant.org

[version]: http://contributor-covenant.org/version/1/4/
[version]: https://contributor-covenant.org/version/1/4/
39 changes: 17 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Matomo Java Tracker
# Official Matomo Java Tracker

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.piwik.java.tracking/matomo-java-tracker/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/org.piwik.java.tracking/matomo-java-tracker)
[![Build Status](https://github.com/matomo-org/matomo-java-tracker/actions/workflows/build.yml/badge.svg)](https://github.com/matomo-org/matomo-java-tracker/actions/workflows/build.yml)
Expand All @@ -17,7 +17,7 @@ Features include:
* Includes tracking parameters for campaigns, events, downloads, outlinks, site search, devices, visiors
* Supports Java 8 and higher
* Allows you to skip SSL certificate validation (not recommended for production)
* Contains nearly no dependencies
* Contains nearly no runtime dependencies (only SLF4J)
* Allows asynchronous requests
* Supports Matomo 4 and 5
* Single and multiple requests can be sent
Expand Down Expand Up @@ -161,7 +161,7 @@ public class YourImplementation {
MatomoRequest request = MatomoRequest
.builder()
.siteId(42)
.actionUrl("http://example.org/landing.html?pk_campaign=Email-Nov2011&pk_kwd=LearnMore") // include the query parameters to the url
.actionUrl("https://example.org/landing.html?pk_campaign=Email-Nov2011&pk_kwd=LearnMore") // include the query parameters to the url
.actionName("LearnMore")
.build();
}
Expand Down Expand Up @@ -229,7 +229,8 @@ The Matomo Tracker currently supports the following builder methods:
port and username must be set as well.
* `.disableSslCertValidation(...)` If set to true, the SSL certificate of the Matomo server will not be validated. This
should only be used for testing purposes. Default: false
* `.disableSslHostVerification(...)` If set to true, the SSL host of the Matomo server will not be validated. This should
* `.disableSslHostVerification(...)` If set to true, the SSL host of the Matomo server will not be validated. This
should
only be used for testing purposes. Default: false

To send a single request synchronously via GET, call
Expand Down Expand Up @@ -262,9 +263,7 @@ public class YourImplementation {
.apiEndpoint(URI.create("https://your-matomo-domain.tld/matomo.php"))
.build());

CompletableFuture<Void> future = tracker.sendRequestAsync(request);
// execute the request:
future.get();
tracker.sendRequestAsync(request);

}

Expand All @@ -277,8 +276,6 @@ send a bulk request. Place your requests in an _Iterable_ data structure and cal
```java
package example;

import java.util.concurrent.CompletableFuture;
import org.apache.http.HttpResponse;
import org.matomo.java.tracking.MatomoRequest;
import org.matomo.java.tracking.MatomoTracker;

Expand All @@ -287,7 +284,6 @@ import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class YourImplementation {

Expand All @@ -303,9 +299,7 @@ public class YourImplementation {
.apiEndpoint(URI.create("https://your-matomo-domain.tld/matomo.php"))
.build());

CompletableFuture<Void> future = tracker.sendBulkRequestAsync(requests);
// execute the request
future.get();
tracker.sendBulkRequestAsync(requests);

}

Expand All @@ -319,7 +313,6 @@ the bulk request through
```java
package example;

import java.util.concurrent.CompletableFuture;
import org.apache.http.HttpResponse;
import org.matomo.java.tracking.MatomoLocale;
import org.matomo.java.tracking.MatomoRequest;
Expand All @@ -331,7 +324,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class YourImplementation {

Expand All @@ -351,12 +343,8 @@ public class YourImplementation {
.apiEndpoint(URI.create("https://your-matomo-domain.tld/matomo.php"))
.build());

CompletableFuture<Void> future = tracker.sendBulkRequestAsync(
requests,
"33dc3f2536d3025974cccb4b4d2d98f4"
); // second parameter is authentication token need for country override
// execute the request:
future.get();
// second parameter is authentication token need for country override
tracker.sendBulkRequestAsync(requests, "33dc3f2536d3025974cccb4b4d2d98f4");


}
Expand Down Expand Up @@ -465,7 +453,7 @@ version can be used in your local Maven repository for testing purposes, e.g.

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see
We use [SemVer](https://semver.org/) for versioning. For the versions available, see
the [tags on this repository](https://github.com/matomo-org/matomo-java-tracker/tags).

## Contribute
Expand All @@ -490,6 +478,13 @@ process for submitting pull requests to us.
We use Checkstyle and JaCoCo to ensure code quality. Please run `mvn verify` before submitting a pull request. Please
provide tests for your changes. We use JUnit 5 for testing. Coverage should be at least 80%.

## Other Java Matomo Tracker Implementations

* [Matomo SDK for Android](https://github.com/matomo-org/matomo-sdk-android)
* [piwik-tracking](https://github.com/ralscha/piwik-tracking)
* [Matomo Tracking API Java Client](https://github.com/dheid/matomo-tracker) -> Most of the code was integrated in the
official Matomo Java Tracker

## License

This software is released under the BSD 3-Clause license. See [LICENSE](LICENSE).
Expand Down
4 changes: 2 additions & 2 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
that can be found at https://google.github.io/styleguide/javaguide.html
Checkstyle is very configurable. Be sure to read the documentation at
http://checkstyle.org (or in your downloaded distribution).
https://checkstyle.org (or in your downloaded distribution).
To completely disable a check, just comment it out or delete it from the file.
To suppress certain violations please review suppression filters.
Expand All @@ -35,7 +35,7 @@
</module>

<!-- Checks for whitespace -->
<!-- See http://checkstyle.org/config_whitespace.html -->
<!-- See https://checkstyle.org/config_whitespace.html -->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/org/matomo/java/tracking/DaemonThreadFactory.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/matomo/java/tracking/MatomoRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public class MatomoRequest {

/**
* The campaign keyword (see
* <a href="http://matomo.org/docs/tracking-campaigns/">Tracking Campaigns</a>). Used to populate the <em>Referrers &gt; Campaigns</em> report (clicking on a
* <a href="https://matomo.org/docs/tracking-campaigns/">Tracking Campaigns</a>). Used to populate the <em>Referrers &gt; Campaigns</em> report (clicking on a
* campaign loads all keywords for this campaign). This parameter will only be used for the first pageview of a visit.
*/
@TrackingParameter(name = "_rck")
Expand Down Expand Up @@ -841,7 +841,7 @@ public MatomoDate getRequestDatetime() {
* Set the datetime of the request (normally the current time is used).
* This can be used to record visits and page views in the past. The datetime
* must be sent in UTC timezone. <em>Note: if you record data in the past, you will
* need to <a href="http://matomo.org/faq/how-to/faq_59">force Matomo to re-process
* need to <a href="https://matomo.org/faq/how-to/faq_59">force Matomo to re-process
* reports for the past dates</a>.</em> If you set the <em>Request Datetime</em> to a datetime
* older than four hours then <em>Auth Token</em> must be set. If you set
* <em>Request Datetime</em> with a datetime in the last four hours then you
Expand Down
24 changes: 7 additions & 17 deletions src/main/java/org/matomo/java/tracking/MatomoTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -35,7 +35,7 @@ public class MatomoTracker {
* Tracking HTTP API endpoint.
*
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-matomo-domain.tld/matomo.php</strong>. Must not be null
* <strong>https://your-matomo-domain.tld/matomo.php</strong>. Must not be null
* @deprecated Please use {@link MatomoTracker#MatomoTracker(TrackerConfiguration)}
*/
@Deprecated
Expand All @@ -51,7 +51,7 @@ public MatomoTracker(
* Tracking HTTP API endpoint.
*
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-matomo-domain.tld/matomo.php</strong>.
* <strong>https://your-matomo-domain.tld/matomo.php</strong>.
* @param timeout the timeout of the sent request in milliseconds or -1 if not set
* @deprecated Please use {@link MatomoTracker#MatomoTracker(TrackerConfiguration)}
*/
Expand All @@ -68,7 +68,7 @@ public MatomoTracker(
* Tracking HTTP API endpoint.
*
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-matomo-domain.tld/matomo.php</strong>.
* <strong>https://your-matomo-domain.tld/matomo.php</strong>.
* @param proxyHost The hostname or IP address of an optional HTTP proxy, null allowed
* @param proxyPort The port of an HTTP proxy or -1 if not set
* @param timeout the timeout of the request in milliseconds or -1 if not set
Expand Down Expand Up @@ -104,28 +104,18 @@ public MatomoTracker(
requireNonNull(trackerConfiguration, "Tracker configuration must not be null");
trackerConfiguration.validate();
this.trackerConfiguration = trackerConfiguration;
ScheduledThreadPoolExecutor threadPoolExecutor = createThreadPoolExecutor();
sender = new Sender(trackerConfiguration,
new QueryCreator(trackerConfiguration),
threadPoolExecutor
);
}

@edu.umd.cs.findbugs.annotations.NonNull
private static ScheduledThreadPoolExecutor createThreadPoolExecutor() {
DaemonThreadFactory threadFactory = new DaemonThreadFactory();
ScheduledThreadPoolExecutor threadPoolExecutor =
new ScheduledThreadPoolExecutor(1, threadFactory);
threadPoolExecutor.setRemoveOnCancelPolicy(true);
return threadPoolExecutor;
Executors.newFixedThreadPool(trackerConfiguration.getThreadPoolSize()
));
}

/**
* Creates a tracker that will send {@link MatomoRequest}s to the specified
* Tracking HTTP API endpoint via the provided proxy.
*
* @param hostUrl url endpoint to send requests to. Usually in the format
* <strong>http://your-matomo-domain.tld/matomo.php</strong>.
* <strong>https://your-matomo-domain.tld/matomo.php</strong>.
* @param proxyHost url endpoint for the proxy, null allowed
* @param proxyPort proxy server port number or -1 if not set
* @deprecated Please use {@link MatomoTracker#MatomoTracker(TrackerConfiguration)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public class TrackerConfiguration {
*/
boolean disableSslHostVerification;

/**
* The thread pool size for the async sender. Defaults to 2.
*
* <p>Attention: If you use this library in a web application, make sure that this thread pool
* does not exceed the thread pool of the web application. Otherwise, you might run into
* problems.
*/
int threadPoolSize = 2;

/**
* Validates the auth token. The auth token must be exactly 32 characters long.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

/**
* Describes the content for the Accept-Language header field that can be overridden by a custom parameter. The format
* is specified in the corresponding <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 Matching of Language Tags</a>
* is specified in the corresponding <a href="https://tools.ietf.org/html/rfc4647">RFC 4647 Matching of Language Tags</a>
*
* <p>Example: "en-US,en;q=0.8,de;q=0.6"
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/matomo/java/tracking/parameters/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

package org.matomo.java.tracking.parameters;

import lombok.NonNull;

final class Hex {

private Hex() {
// utility class
}

static String fromBytes(byte[] bytes) {
static String fromBytes(@NonNull byte[] bytes) {
StringBuilder result = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
result.append(String.format("%02x", b));
Expand Down
Loading

0 comments on commit 7e410ea

Please sign in to comment.