Skip to content

Commit aaf0aa4

Browse files
author
David Witherspoon
authored
Merge pull request #14 from zahariaca/fixForJavaDemosWSSProblem
Fix for Java demos, WSS issues 571, 972 & 973
2 parents 7463a54 + ef31a5e commit aaf0aa4

File tree

13 files changed

+399
-204
lines changed

13 files changed

+399
-204
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ out
1717
build-local.properties
1818
.gradle
1919
build
20+
gradle
21+
gradlew
22+
gradlew.bat
2023

2124
# Github java basic
2225
*.class

j2se/java-amqp-demo/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This J2SE console app communicates over WebSocket with an AMQP server via Kaazin
99

1010
## Steps for Building and Running the Project
1111

12+
- Install gradle: follow the steps [here](https://gradle.org/gradle-download/).
13+
1214
- Build the application using gradle
1315

1416
```bash
@@ -25,6 +27,25 @@ or
2527
```
2628
build\install\java-amqp-demo\bin\java-amqp-demo.bat
2729
```
30+
31+
**NOTE:** The application can be run in the folowing ways:
32+
- To connect to our default URI and default credentials (guest/guest):
33+
```
34+
/java-amqp-demo
35+
```
36+
- To connect to your own local Kaazing Gateway URI (ex: *ws://localhost:8000/zmqp*):
37+
```
38+
/java-jms-demo '{YOUR.GATEWAY.URI}'
39+
```
40+
- To use credentials with our default URI:
41+
```
42+
/java-aqmp-demo 'guest' 'guest'
43+
```
44+
- If you have setup your gateway for authentification:
45+
```
46+
/java-jms-demo '{YOUR.GATEWAY.URI}' '{USERNAME}' '{PASSWORD}'
47+
```
48+
2849
**Note:** If you encounter an exception, try running the program as the root user (`sudo`).
2950

3051
## Interact with Kaazing Java AMQP Client API

j2se/java-amqp-demo/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'eclipse'
33
apply plugin: 'application'
44

55
sourceCompatibility = 1.8
6-
mainClassName = 'com.kaazing.amqp.client.demo.JavaAmqpClientDemo'
6+
mainClassName = 'com.kaazing.amqp.client.demo.AmqpDemoClientMain'
77

88

99
repositories {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
9+
public class AmqpDemoClientMain {
10+
public static void main(String[] args) throws InterruptedException, URISyntaxException, IOException {
11+
JavaAmqpClientDemo demo = null;
12+
switch (args.length){
13+
case 0:
14+
demo = new JavaAmqpClientDemo("ws://sandbox.kaazing.net/amqp091", "guest", "guest");
15+
break;
16+
case 1:
17+
demo = new JavaAmqpClientDemo(args[0], "guest", "guest");
18+
break;
19+
case 2:
20+
demo = new JavaAmqpClientDemo("ws://sandbox.kaazing.net/jms", args[0], args[1]);
21+
break;
22+
case 3:
23+
demo = new JavaAmqpClientDemo(args[0], args[1], args[2]);
24+
break;
25+
default:
26+
System.out.println("Possible usage of the Kaazing Java JMS Demo: \n" +
27+
" 1. If you want to connect to our default URI and default credentials (guest/guest): \n" +
28+
" /java-amqp-demo\n" +
29+
" 2. If you want to connect to your own local Kaazing Gateway URI (ex: *ws://localhost:8000/jms*):\n" +
30+
" /java-amqp-demo '{YOUR.GATEWAY.URI}' \n" +
31+
" 3. If you want to use authentication with our default URI: \n" +
32+
" /java-aqmp-demo 'username' `password` \n" +
33+
" 4. If you have setup your gateway for authentication: \n" +
34+
" /java-amqp-demo '{YOUR.GATEWAY.URI}' '{USERNAME}' '{PASSWORD}' \n" +
35+
"Please restart your the Kaazing Java AMQP Demo and input the correct parameters as stated above!");
36+
System.exit(1);
37+
}
38+
demo.handleConnection();
39+
System.out.println("Kaazing Java AMQP Demo App. Copyright (C) 2017 Kaazing, Inc.");
40+
System.out.println("Type the message to send or <exit> to stop.");
41+
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
42+
while (true) {
43+
String text = console.readLine();
44+
if (text.equalsIgnoreCase("<exit>"))
45+
break;
46+
// Send as a text
47+
demo.sendMessage(text);
48+
}
49+
demo.disconnect();
50+
}
51+
}

j2se/java-amqp-demo/src/main/java/com/kaazing/amqp/client/demo/JavaAmqpClientDemo.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.kaazing.net.ws.amqp.ConnectionEvent;
2323
import com.kaazing.net.ws.amqp.ConnectionListener;
2424

25+
import javax.net.ssl.SSLHandshakeException;
26+
2527
public class JavaAmqpClientDemo {
2628
private AmqpClient amqpClient;
2729
private AmqpChannel publishChannel = null;
@@ -31,10 +33,19 @@ public class JavaAmqpClientDemo {
3133
private final String myConsumerTag = "clientkey";
3234
private final String routingKey = "broadcastkey";
3335
private final String virtualHost = "/";
34-
private String login;
36+
private final String url;
37+
private final String login;
38+
private final String password;
39+
3540

36-
public JavaAmqpClientDemo(URI url, String login, String password) throws InterruptedException {
41+
public JavaAmqpClientDemo(String url, String login, String password) throws InterruptedException {
42+
this.url = url;
3743
this.login = login;
44+
this.password = password;
45+
}
46+
47+
public void handleConnection() throws InterruptedException {
48+
3849
AmqpClientFactory amqpClientFactory = AmqpClientFactory.createAmqpClientFactory();
3950
amqpClient = amqpClientFactory.createAmqpClient();
4051
System.out.println("CONNECTING: " + url + " " + login + "/" + password);
@@ -62,6 +73,7 @@ public void onConnectionClose(ConnectionEvent e) {
6273
if (consumeChannel != null) {
6374
consumeChannel.closeChannel(0, "", 0, 0);
6475
}
76+
System.exit(0);
6577

6678
}
6779

@@ -70,7 +82,7 @@ public void onConnectionError(ConnectionEvent e) {
7082
System.exit(-1);
7183
}
7284
});
73-
amqpClient.connect(url.toString(), virtualHost, login, password);
85+
amqpClient.connect(url, virtualHost, login, password);
7486
connectionLatch.await(10, TimeUnit.SECONDS);
7587

7688
final CountDownLatch pubChannelLatch = new CountDownLatch(1);
@@ -119,7 +131,8 @@ public void onClose(ChannelEvent e) {
119131

120132
@Override
121133
public void onConsumeBasic(ChannelEvent e) {
122-
System.out.println("CONSUME FROM QUEUE: " + queueName);
134+
System.out.println("CONSUME FROM QUEUE: " + queueName +"\n");
135+
System.out.print("User input: ");
123136
}
124137

125138
@Override
@@ -141,15 +154,15 @@ public void onMessage(final ChannelEvent e) {
141154
final Long dt = (Long) e.getArgument("deliveryTag");
142155
final String value = new String(bytes, Charset.forName("UTF-8"));
143156

144-
System.out.println(">>> MESSAGE RECEIVED: " + value);
157+
System.out.println("<- MESSAGE RECEIVED: " + value);
145158
AmqpProperties props = e.getAmqpProperties();
146159
if (props != null) {
147160
AmqpArguments headers = props.getHeaders();
148-
161+
System.out.println("Amqp properties: ");
149162
if (headers != null) {
150-
System.out.println("Headers: " + headers.toString());
163+
System.out.println("- Headers: " + headers.toString());
151164
}
152-
System.out.println("Properties " + (String) props.toString());
165+
System.out.println("- Properties " + (String) props.toString());
153166

154167
// Acknowledge the message as we passed in a 'false' for
155168
// noAck in AmqpChannel.consumeBasic() call. If the
@@ -160,6 +173,7 @@ public void onMessage(final ChannelEvent e) {
160173
AmqpChannel channel = e.getChannel();
161174
channel.ackBasic(dt.longValue(), true);
162175
}
176+
System.out.print("\nUser input: ");
163177
}
164178

165179
@Override
@@ -177,6 +191,11 @@ public void disconnect() {
177191
}
178192

179193
public void sendMessage(String message) {
194+
// This check needs to be done, otherwise if the user would hit enter without a message,
195+
// nothing would be sent and the program would disconnect and terminate
196+
if(message.equals("")){
197+
message = " ";
198+
}
180199

181200
ByteBuffer buffer = ByteBuffer.allocate(512);
182201
buffer.put(message.getBytes(Charset.forName("UTF-8")));
@@ -201,22 +220,6 @@ public void sendMessage(String message) {
201220
props.setHeaders(customHeaders);
202221

203222
publishChannel.publishBasic(buffer, props, exchangeName, routingKey, false, false);
204-
System.out.println("MESSAGE PUBLISHED: " + message);
223+
System.out.println("-> MESSAGE PUBLISHED: " + message);
205224
}
206-
207-
public static void main(String[] args) throws InterruptedException, URISyntaxException, IOException {
208-
JavaAmqpClientDemo demo = new JavaAmqpClientDemo(new URI("wss://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-
222225
}

j2se/java-jms-demo/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This J2SE console app communicates over WebSocket with a JMS server via Kaazing
99

1010
## Steps for building and running the project
1111

12+
- Install gradle: follow the steps [here](https://gradle.org/gradle-download/).
13+
1214
- Build the application using gradle
1315

1416
```bash
@@ -24,6 +26,23 @@ or
2426
```
2527
build\install\java-jms-demo\bin\java-jms-demo.bat
2628
```
29+
**NOTE:** The application can be run in the folowing ways:
30+
- To connect to our defult URI:
31+
```
32+
/java-jms-demo
33+
```
34+
-To your own local Kaazing Gateway URI (ex: *ws://localhost:8000/jms*):
35+
```
36+
/java-jms-demo '{YOUR.GATEWAY.URI}'
37+
```
38+
- To use credentials with our default URI:
39+
```
40+
/java-jms-demo 'joe' 'welcome'
41+
```
42+
- If you have setup your gateway for authentification :
43+
```
44+
/java-jms-demo '{YOUR.GATEWAY.URI}' '{USERNAME}' '{PASSWORD}'
45+
```
2746

2847
## Interact with Kaazing Java WebSocket Client API
2948

j2se/java-jms-demo/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'eclipse'
33
apply plugin: 'application'
44

55
sourceCompatibility = 1.8
6-
mainClassName = 'com.kaazing.jms.client.demo.JavaJMSClientDemo'
6+
mainClassName = 'com.kaazing.jms.client.demo.JMSDemoClientMain'
77

88

99
repositories {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.kaazing.jms.client.demo;
2+
3+
import javax.jms.JMSException;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.net.URI;
8+
import java.net.URISyntaxException;
9+
10+
/**
11+
* Created by azaharia on 02.02.2017.
12+
*/
13+
public class JMSDemoClientMain {
14+
15+
public static void main(String[] args) throws InterruptedException, URISyntaxException, IOException, JMSException {
16+
JavaJMSClientDemo demo = null;
17+
switch (args.length){
18+
case 0:
19+
demo = new JavaJMSClientDemo(new URI("ws://sandbox.kaazing.net/jms"), "", "");
20+
break;
21+
case 1:
22+
demo = new JavaJMSClientDemo(new URI(args[0]), "", "");
23+
break;
24+
case 2:
25+
demo = new JavaJMSClientDemo(new URI("ws://sandbox.kaazing.net/jms"), args[0], args[1]);
26+
break;
27+
case 3:
28+
demo = new JavaJMSClientDemo(new URI(args[0]), args[1], args[2]);
29+
break;
30+
default:
31+
System.out.println("Possible usage of the Kaazing Java JMS Demo: \n" +
32+
" 1. If you want to connect to our default URI: \n" +
33+
" /java-jms-demo\n" +
34+
" 2. If you want to connect to your own local Kaazing Gateway URI (ex: *ws://localhost:8000/jms*):\n" +
35+
" /java-jms-demo '{YOUR.GATEWAY.URI}' \n" +
36+
" 3. If you want to use authentication with our default URI: \n" +
37+
" /java-jms-demo 'joe' `welcome` \n" +
38+
" 4. If you have setup your gateway for authentication: \n" +
39+
" /java-jms-demo '{YOUR.GATEWAY.URI}' '{USERNAME}' '{PASSWORD}' \n" +
40+
"Please restart your the Kaazing Java JMS Demo and input the correct parameters as stated above!");
41+
System.exit(1);
42+
}
43+
demo.handleConnection();
44+
System.out.println("Kaazing Java JMS Demo App. Copyright (C) 2017 Kaazing, Inc.");
45+
System.out.println("Type the message to send or <exit> to stop.");
46+
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
47+
System.out.print("User input: ");
48+
while (true) {
49+
String text = console.readLine();
50+
if (text.equalsIgnoreCase("<exit>"))
51+
break;
52+
// Send as a text
53+
demo.sendMessage(text);
54+
}
55+
demo.disconnect();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)