Skip to content

Commit 50cbb17

Browse files
authored
Merge pull request #269 from segmentio/repo-sync
repo sync
2 parents 468baf1 + c6590e3 commit 50cbb17

File tree

3 files changed

+92
-16
lines changed

3 files changed

+92
-16
lines changed

src/connections/sources/catalog/libraries/server/java/index.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Add to `pom.xml`:
3838
or if you're using Gradle:
3939

4040
```bash
41-
compile 'com.segment.analytics.java:analytics:+'
41+
implementation 'com.segment.analytics.java:analytics:+'
4242
```
4343

4444
### Initialize the SDK
@@ -74,14 +74,13 @@ We recommend calling `identify` a single time when the user's account is first c
7474
Example `identify` call:
7575

7676
```java
77+
Map<String, String> map = new HashMap();
78+
map.put("name", "Michael Bolton");
79+
map.put("email", "[email protected]");
80+
7781
analytics.enqueue(IdentifyMessage.builder()
78-
.userId("f4ca124298")
79-
.traits(ImmutableMap.builder()
80-
.put("name", "Michael Bolton")
81-
.put("email", "[email protected]")
82-
.build()
83-
)
84-
);
82+
.userId("f4ca124298")
83+
.traits(map));
8584
```
8685

8786
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.

src/connections/sources/catalog/libraries/server/java/quickstart.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Here's what it would look like with Maven:
4040
*or if you're using Gradle:*
4141

4242
```bash
43-
compile 'com.segment.analytics.java:analytics:+'
43+
implementation 'com.segment.analytics.java:analytics:+'
4444
```
4545

4646
## Step 3: Initialize the SDK
@@ -71,14 +71,13 @@ The `identify` message is how you tell Segment who the current user is. It inclu
7171
Here's what a basic call to `identify` a user might look like:
7272

7373
```java
74+
Map<String, String> map = new HashMap();
75+
map.put("name", "Michael Bolton");
76+
map.put("email", "[email protected]");
77+
7478
analytics.enqueue(IdentifyMessage.builder()
75-
.userId("f4ca124298")
76-
.traits(ImmutableMap.builder()
77-
.put("name", "Michael Bolton")
78-
.put("email", "[email protected]")
79-
.build()
80-
)
81-
);
79+
.userId("f4ca124298")
80+
.traits(map));
8281
```
8382

8483
**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. <!-- LR: Can't find that we ever had a doc about this. Read more about it in the [transformer reference docs](/docs/connections/sources/catalog/libraries/server/java#transformer).-->

src/connections/sources/catalog/libraries/server/node/index.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,84 @@ analytics.flush(function(err, batch){
437437
});
438438
```
439439

440+
## Long running process
441+
442+
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.
443+
444+
```js
445+
import { randomUUID } from 'crypto';
446+
import Analytics from 'analytics-node'
447+
448+
const WRITE_KEY = '...';
449+
450+
const analytics = new Analytics(WRITE_KEY, { flushAt: 10 });
451+
452+
analytics.track({
453+
anonymousId: randomUUID(),
454+
event: 'Test event',
455+
properties: {
456+
name: 'Test event',
457+
timestamp: new Date()
458+
}
459+
});
460+
461+
const exitGracefully = async (code) => {
462+
console.log('Flushing events');
463+
await analytics.flush(function(err, batch) {
464+
console.log('Flushed, and now this program can exit!');
465+
process.exit(code);
466+
});
467+
};
468+
469+
[
470+
'beforeExit', 'uncaughtException', 'unhandledRejection',
471+
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP',
472+
'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV',
473+
'SIGUSR2', 'SIGTERM',
474+
].forEach(evt => process.on(evt, exitGracefully));
475+
476+
function logEvery2Seconds(i) {
477+
setTimeout(() => {
478+
console.log('Infinite Loop Test n:', i);
479+
logEvery2Seconds(++i);
480+
}, 2000);
481+
}
482+
483+
logEvery2Seconds(0);
484+
```
485+
486+
## Short lived process
487+
488+
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.
489+
490+
491+
```js
492+
import { randomUUID } from 'crypto';
493+
import Analytics from 'analytics-node'
494+
495+
496+
async function lambda()
497+
{
498+
const WRITE_KEY = '...';
499+
const analytics = new Analytics(WRITE_KEY, { flushAt: 20 });
500+
analytics.flushed = true;
501+
502+
analytics.track({
503+
anonymousId: randomUUID(),
504+
event: 'Test event',
505+
properties: {
506+
name: 'Test event',
507+
timestamp: new Date()
508+
}
509+
});
510+
await analytics.flush(function(err, batch) {
511+
console.log('Flushed, and now this program can exit!');
512+
});
513+
}
514+
515+
lambda();
516+
```
517+
440518

441519
## Multiple Clients
442520

0 commit comments

Comments
 (0)