diff --git a/src/connections/sources/catalog/libraries/server/java/index.md b/src/connections/sources/catalog/libraries/server/java/index.md index 7bdee1de35..153dc32e69 100644 --- a/src/connections/sources/catalog/libraries/server/java/index.md +++ b/src/connections/sources/catalog/libraries/server/java/index.md @@ -38,7 +38,7 @@ Add to `pom.xml`: or if you're using Gradle: ```bash -compile 'com.segment.analytics.java:analytics:+' +implementation 'com.segment.analytics.java:analytics:+' ``` ### Initialize the SDK @@ -74,14 +74,13 @@ We recommend calling `identify` a single time when the user's account is first c Example `identify` call: ```java +Map map = new HashMap(); +map.put("name", "Michael Bolton"); +map.put("email", "mbolton@example.com"); + analytics.enqueue(IdentifyMessage.builder() - .userId("f4ca124298") - .traits(ImmutableMap.builder() - .put("name", "Michael Bolton") - .put("email", "mbolton@example.com") - .build() - ) -); + .userId("f4ca124298") + .traits(map)); ``` This call is identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with `name` and `email` traits. diff --git a/src/connections/sources/catalog/libraries/server/java/quickstart.md b/src/connections/sources/catalog/libraries/server/java/quickstart.md index 35edefb988..97666556f2 100644 --- a/src/connections/sources/catalog/libraries/server/java/quickstart.md +++ b/src/connections/sources/catalog/libraries/server/java/quickstart.md @@ -40,7 +40,7 @@ Here's what it would look like with Maven: *or if you're using Gradle:* ```bash -compile 'com.segment.analytics.java:analytics:+' +implementation 'com.segment.analytics.java:analytics:+' ``` ## Step 3: Initialize the SDK @@ -71,14 +71,13 @@ The `identify` message is how you tell Segment who the current user is. It inclu Here's what a basic call to `identify` a user might look like: ```java +Map map = new HashMap(); +map.put("name", "Michael Bolton"); +map.put("email", "mbolton@example.com"); + analytics.enqueue(IdentifyMessage.builder() - .userId("f4ca124298") - .traits(ImmutableMap.builder() - .put("name", "Michael Bolton") - .put("email", "mbolton@example.com") - .build() - ) -); + .userId("f4ca124298") + .traits(map)); ``` **Note:** The enqueue method takes a `MessageBuilder` instance and not a `Message` instance directly. This is to allow you to use a `MessageTransformer` that applies to all incoming messages and transform or add data. diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index 730e65f7d3..30b6b85f86 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -437,6 +437,84 @@ analytics.flush(function(err, batch){ }); ``` +## Long running process + +You should call `client.track(...)` and know that events will be queued and eventually sent to Segment. To prevent losing messages, be sure to capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown. + +```js +import { randomUUID } from 'crypto'; +import Analytics from 'analytics-node' + +const WRITE_KEY = '...'; + +const analytics = new Analytics(WRITE_KEY, { flushAt: 10 }); + +analytics.track({ + anonymousId: randomUUID(), + event: 'Test event', + properties: { + name: 'Test event', + timestamp: new Date() + } +}); + +const exitGracefully = async (code) => { + console.log('Flushing events'); + await analytics.flush(function(err, batch) { + console.log('Flushed, and now this program can exit!'); + process.exit(code); + }); +}; + +[ + 'beforeExit', 'uncaughtException', 'unhandledRejection', + 'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', + 'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', + 'SIGUSR2', 'SIGTERM', +].forEach(evt => process.on(evt, exitGracefully)); + +function logEvery2Seconds(i) { + setTimeout(() => { + console.log('Infinite Loop Test n:', i); + logEvery2Seconds(++i); + }, 2000); +} + +logEvery2Seconds(0); +``` + +## Short lived process + +Short-lived functions have a predictably short and linear lifecycle, so use a queue big enough to hold all messages and then await flush to complete its work. + + +```js +import { randomUUID } from 'crypto'; +import Analytics from 'analytics-node' + + +async function lambda() +{ + const WRITE_KEY = '...'; + const analytics = new Analytics(WRITE_KEY, { flushAt: 20 }); + analytics.flushed = true; + + analytics.track({ + anonymousId: randomUUID(), + event: 'Test event', + properties: { + name: 'Test event', + timestamp: new Date() + } + }); + await analytics.flush(function(err, batch) { + console.log('Flushed, and now this program can exit!'); + }); +} + +lambda(); +``` + ## Multiple Clients