-
Notifications
You must be signed in to change notification settings - Fork 915
/
Copy pathBasicPublisher.java
132 lines (117 loc) · 5.23 KB
/
BasicPublisher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2014-2025 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.aeron.samples;
import io.aeron.Aeron;
import io.aeron.Publication;
import io.aeron.driver.MediaDriver;
import org.agrona.BufferUtil;
import org.agrona.CloseHelper;
import org.agrona.concurrent.UnsafeBuffer;
import java.util.concurrent.TimeUnit;
/**
* Basic Aeron publisher application.
* <p>
* This publisher sends a fixed number of messages on a channel and stream ID,
* then lingers to allow any consumers that may have experienced loss a chance to NAK for
* and recover any missing data.
* The default values for number of messages, channel, and stream ID are
* defined in {@link SampleConfiguration} and can be overridden by
* setting their corresponding properties via the command-line; e.g.:
* -Daeron.sample.channel=aeron:udp?endpoint=localhost:5555 -Daeron.sample.streamId=20
*/
public class BasicPublisher
{
private static final int STREAM_ID = SampleConfiguration.STREAM_ID;
private static final String CHANNEL = SampleConfiguration.CHANNEL;
private static final long NUMBER_OF_MESSAGES = SampleConfiguration.NUMBER_OF_MESSAGES;
private static final long LINGER_TIMEOUT_MS = SampleConfiguration.LINGER_TIMEOUT_MS;
private static final boolean EMBEDDED_MEDIA_DRIVER = SampleConfiguration.EMBEDDED_MEDIA_DRIVER;
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread sleep delay is interrupted.
*/
public static void main(final String[] args) throws InterruptedException
{
System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);
// If configured to do so, create an embedded media driver within this application rather
// than relying on an external one.
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context();
if (EMBEDDED_MEDIA_DRIVER)
{
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
// Connect a new Aeron instance to the media driver and create a publication on
// the given channel and stream ID.
// The Aeron and Publication classes implement "AutoCloseable" and will automatically
// clean up resources when this try block is finished
try (Aeron aeron = Aeron.connect(ctx);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
{
final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(256, 64));
for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
{
System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");
final int length = buffer.putStringWithoutLengthAscii(0, "Hello World! " + i);
final long position = publication.offer(buffer, 0, length);
if (position > 0)
{
System.out.println("yay!");
}
else if (position == Publication.BACK_PRESSURED)
{
System.out.println("Offer failed due to back pressure");
}
else if (position == Publication.NOT_CONNECTED)
{
System.out.println("Offer failed because publisher is not connected to a subscriber");
}
else if (position == Publication.ADMIN_ACTION)
{
System.out.println("Offer failed because of an administration action in the system");
}
else if (position == Publication.CLOSED)
{
System.out.println("Offer failed because publication is closed");
break;
}
else if (position == Publication.MAX_POSITION_EXCEEDED)
{
System.out.println("Offer failed due to publication reaching its max position");
break;
}
else
{
System.out.println("Offer failed due to unknown reason: " + position);
}
if (!publication.isConnected())
{
System.out.println("No active subscribers detected");
}
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
System.out.println("Done sending.");
if (LINGER_TIMEOUT_MS > 0)
{
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
}
CloseHelper.close(driver);
}
}