Skip to content

Commit 92422a2

Browse files
committed
Added demos for j2se
1 parent 5bd60d9 commit 92422a2

6 files changed

Lines changed: 600 additions & 0 deletions

File tree

j2se/java-amqp-demo/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'java'
2+
apply plugin: 'eclipse'
3+
4+
sourceCompatibility = 1.5
5+
version = '1.0'
6+
jar {
7+
manifest {
8+
attributes 'Implementation-Title': 'Kaazing AMQP Java Client Demo',
9+
'Implementation-Version': version
10+
}
11+
}
12+
13+
repositories {
14+
mavenCentral()
15+
maven {
16+
url "http://artifactory.kaazing.wan/artifactory/libs-releases-local"
17+
}
18+
}
19+
20+
dependencies {
21+
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
22+
compile group: 'com.kaazing', name: 'enterprise.java.client.all', version: '4.1.0-RC006'
23+
testCompile group: 'junit', name: 'junit', version: '4.+'
24+
}
25+
26+
test {
27+
systemProperties 'property': 'value'
28+
}
29+
30+
uploadArchives {
31+
repositories {
32+
flatDir {
33+
dirs 'repos'
34+
}
35+
}
36+
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package com.kaazing.amqp.client.demo;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
import java.nio.ByteBuffer;
9+
import java.nio.charset.Charset;
10+
import java.sql.Timestamp;
11+
import java.util.Random;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import com.kaazing.net.ws.amqp.AmqpArguments;
16+
import com.kaazing.net.ws.amqp.AmqpChannel;
17+
import com.kaazing.net.ws.amqp.AmqpClient;
18+
import com.kaazing.net.ws.amqp.AmqpClientFactory;
19+
import com.kaazing.net.ws.amqp.AmqpProperties;
20+
import com.kaazing.net.ws.amqp.ChannelAdapter;
21+
import com.kaazing.net.ws.amqp.ChannelEvent;
22+
import com.kaazing.net.ws.amqp.ConnectionEvent;
23+
import com.kaazing.net.ws.amqp.ConnectionListener;
24+
25+
public class JavaAmqpClientDemo {
26+
private AmqpClient amqpClient;
27+
private AmqpChannel publishChannel = null;
28+
private AmqpChannel consumeChannel = null;
29+
private final String queueName = "queue" + new Random().nextInt();
30+
private final String exchangeName = "demo_exchange";
31+
private final String myConsumerTag = "clientkey";
32+
private final String routingKey = "broadcastkey";
33+
private final String virtualHost = "/";
34+
private String login;
35+
36+
public JavaAmqpClientDemo(URI url, String login, String password) throws InterruptedException {
37+
this.login = login;
38+
AmqpClientFactory amqpClientFactory = AmqpClientFactory.createAmqpClientFactory();
39+
amqpClient = amqpClientFactory.createAmqpClient();
40+
System.out.println("CONNECTING: " + url + " " + login + "/" + password);
41+
42+
final CountDownLatch connectionLatch = new CountDownLatch(1);
43+
amqpClient.addConnectionListener(new ConnectionListener() {
44+
45+
public void onConnectionOpen(ConnectionEvent e) {
46+
System.out.println("CONNECTED...");
47+
connectionLatch.countDown();
48+
49+
}
50+
51+
public void onConnecting(ConnectionEvent e) {
52+
System.out.println("CONNECTING...");
53+
54+
}
55+
56+
public void onConnectionClose(ConnectionEvent e) {
57+
System.out.println("DISCONNECTING...");
58+
if (publishChannel != null) {
59+
publishChannel.closeChannel(0, "", 0, 0);
60+
}
61+
62+
if (consumeChannel != null) {
63+
consumeChannel.closeChannel(0, "", 0, 0);
64+
}
65+
66+
}
67+
68+
public void onConnectionError(ConnectionEvent e) {
69+
System.err.println("CONNECTION ERROR! " + e.getMessage());
70+
System.exit(-1);
71+
}
72+
});
73+
amqpClient.connect(url.toString(), virtualHost, login, password);
74+
connectionLatch.await(10, TimeUnit.SECONDS);
75+
76+
final CountDownLatch pubChannelLatch = new CountDownLatch(1);
77+
System.out.println("OPEN: Publish Channel");
78+
publishChannel = amqpClient.openChannel();
79+
publishChannel.addChannelListener(new ChannelAdapter() {
80+
@Override
81+
public void onClose(ChannelEvent e) {
82+
System.out.println("CLOSED: Publish Channel");
83+
}
84+
85+
@Override
86+
public void onError(final ChannelEvent e) {
87+
System.err.println("ERROR: Publish Channel - " + e.getMessage());
88+
amqpClient.disconnect();
89+
System.exit(-1);
90+
}
91+
92+
@Override
93+
public void onDeclareExchange(ChannelEvent e) {
94+
System.out.println("EXCHANGE DECLARED: " + exchangeName);
95+
}
96+
97+
@Override
98+
public void onOpen(ChannelEvent e) {
99+
System.out.println("OPENED: Publish Channel");
100+
publishChannel.declareExchange(exchangeName, "fanout", false, false, false, null);
101+
pubChannelLatch.countDown();
102+
}
103+
});
104+
pubChannelLatch.await(10, TimeUnit.SECONDS);
105+
System.out.println("OPEN: Consume Channel");
106+
consumeChannel = amqpClient.openChannel();
107+
final CountDownLatch consumeChannelLatch = new CountDownLatch(1);
108+
consumeChannel.addChannelListener(new ChannelAdapter() {
109+
@Override
110+
public void onBindQueue(ChannelEvent e) {
111+
112+
System.out.println("QUEUE BOUND: " + exchangeName + " - " + queueName);
113+
}
114+
115+
@Override
116+
public void onClose(ChannelEvent e) {
117+
System.out.println("CLOSED: Consume Channel");
118+
}
119+
120+
@Override
121+
public void onConsumeBasic(ChannelEvent e) {
122+
System.out.println("CONSUME FROM QUEUE: " + queueName);
123+
}
124+
125+
@Override
126+
public void onDeclareQueue(ChannelEvent e) {
127+
System.out.println("QUEUE DECLARED: " + queueName);
128+
}
129+
130+
@Override
131+
public void onError(final ChannelEvent e) {
132+
System.err.println("ERROR: Consume Channel - " + e.getMessage());
133+
amqpClient.disconnect();
134+
System.exit(-1);
135+
}
136+
137+
@Override
138+
public void onMessage(final ChannelEvent e) {
139+
byte[] bytes = new byte[e.getBody().remaining()];
140+
e.getBody().get(bytes);
141+
final Long dt = (Long) e.getArgument("deliveryTag");
142+
final String value = new String(bytes, Charset.forName("UTF-8"));
143+
144+
System.out.println(">>> MESSAGE RECEIVED: " + value);
145+
AmqpProperties props = e.getAmqpProperties();
146+
if (props != null) {
147+
AmqpArguments headers = props.getHeaders();
148+
149+
if (headers != null) {
150+
System.out.println("Headers: " + headers.toString());
151+
}
152+
System.out.println("Properties " + (String) props.toString());
153+
154+
// Acknowledge the message as we passed in a 'false' for
155+
// noAck in AmqpChannel.consumeBasic() call. If the
156+
// message is not acknowledged, the broker will keep
157+
// holding the message. And, as more and more messages
158+
// are held by the broker, it will eventually result in
159+
// an OutOfMemoryError.
160+
AmqpChannel channel = e.getChannel();
161+
channel.ackBasic(dt.longValue(), true);
162+
}
163+
}
164+
165+
@Override
166+
public void onOpen(ChannelEvent e) {
167+
System.out.println("OPENED: Consume Channel");
168+
consumeChannel.declareQueue(queueName, false, false, false, false, false, null).bindQueue(queueName, exchangeName, routingKey, false, null).consumeBasic(queueName, myConsumerTag, false, false, false, false, null);
169+
consumeChannelLatch.countDown();
170+
}
171+
});
172+
consumeChannelLatch.await(10, TimeUnit.SECONDS);
173+
}
174+
175+
public void disconnect() {
176+
this.amqpClient.disconnect();
177+
}
178+
179+
public void sendMessage(String message) {
180+
181+
ByteBuffer buffer = ByteBuffer.allocate(512);
182+
buffer.put(message.getBytes(Charset.forName("UTF-8")));
183+
buffer.flip();
184+
185+
Timestamp ts = new Timestamp(System.currentTimeMillis());
186+
AmqpProperties props = new AmqpProperties();
187+
props.setMessageId("1");
188+
props.setCorrelationId("4");
189+
props.setAppId("AMQPDemo");
190+
props.setUserId(this.login);
191+
props.setContentType("text/plain");
192+
props.setContentEncoding("UTF-8");
193+
props.setPriority(6);
194+
props.setDeliveryMode(1);
195+
props.setTimestamp(ts);
196+
197+
AmqpArguments customHeaders = new AmqpArguments();
198+
customHeaders.addInteger("headerKey1", 100);
199+
customHeaders.addLongString("headerKey2", "Header value");
200+
201+
props.setHeaders(customHeaders);
202+
203+
publishChannel.publishBasic(buffer, props, exchangeName, routingKey, false, false);
204+
System.out.println("MESSAGE PUBLISHED: " + message);
205+
}
206+
207+
public static void main(String[] args) throws InterruptedException, URISyntaxException, IOException {
208+
JavaAmqpClientDemo demo = new JavaAmqpClientDemo(new URI("ws://sandbox.kaazing.net/amqp091"), "guest", "guest");
209+
System.out.println("Kaazing Java AMQP Demo App. Copyright (C) 2016 Kaazing, Inc.");
210+
System.out.println("Type the message to send or <exit> to stop.");
211+
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
212+
while (true) {
213+
String text = console.readLine();
214+
if (text.toLowerCase().equals("<exit>"))
215+
break;
216+
// Send as a text
217+
demo.sendMessage(text);
218+
}
219+
demo.disconnect();
220+
}
221+
222+
}

j2se/java-jms-demo/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'java'
2+
apply plugin: 'eclipse'
3+
4+
sourceCompatibility = 1.5
5+
version = '1.0'
6+
jar {
7+
manifest {
8+
attributes 'Implementation-Title': 'Kaazing JMS Java Client Demo',
9+
'Implementation-Version': version
10+
}
11+
}
12+
13+
repositories {
14+
mavenCentral()
15+
maven {
16+
url "http://artifactory.kaazing.wan/artifactory/libs-releases-local"
17+
}
18+
}
19+
20+
dependencies {
21+
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
22+
compile group: 'com.kaazing', name: 'enterprise.java.client.all', version: '4.1.0-RC006'
23+
testCompile group: 'junit', name: 'junit', version: '4.+'
24+
}
25+
26+
test {
27+
systemProperties 'property': 'value'
28+
}
29+
30+
uploadArchives {
31+
repositories {
32+
flatDir {
33+
dirs 'repos'
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)