Added samples in multiple languages

This patch adds samples in Java, JavaScript and Python.

Change-Id: I70cd618d88bb905d6d71a08730b83015f6aaf009
Closes-Bug: #1559613
This commit is contained in:
Fernando Ribeiro 2016-03-19 23:22:45 -03:00
parent 0a191c26c5
commit 0810e1203e
12 changed files with 427 additions and 0 deletions

View File

@ -0,0 +1,43 @@
/*
* 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
*
* http://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 org.openstack.zaqar.sample;
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonObject;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
public final class JsonDecoder implements Decoder.Text<JsonObject> {
@Override
public JsonObject decode(final String s) {
return Json.createReader(new StringReader(s)).readObject();
}
@Override
public void destroy() {
}
@Override
public void init(final EndpointConfig config) {
}
@Override
public boolean willDecode(final String s) {
return true;
}
}

View File

@ -0,0 +1,57 @@
/*
* 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
*
* http://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.
*/
import static java.lang.System.out;
import java.io.IOException;
import javax.json.JsonObject;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
@ClientEndpoint(decoders = JsonDecoder.class)
public final class SampleZaqarEndpoint {
@OnMessage
public void onMessage(final JsonObject msg) {
if (msg.getJsonObject("body").getJsonArray("messages") != null)
out.println(msg.getJsonObject("body").getJsonArray("messages")
.getJsonObject(0).getString("body"));
}
@OnOpen
public void onOpen(final Session sess) throws IOException {
final RemoteEndpoint.Basic remote = sess.getBasicRemote();
final String authenticateMsg = "{\"action\":\"authenticate\","
+ "\"headers\":{\"X-Auth-Token\":"
+ "\"8444886dd9b04a1b87ddb502b508261c\",\"X-Project-ID\":"
+ "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; // refer to bug
// #1553398
remote.sendText(authenticateMsg);
final String claimCreateMsg = "{\"action\":\"claim_create\",\"body\":"
+ "{\"queue_name\":\"SampleQueue\"},\"headers\":{\"Client-ID\":"
+ "\"355186cd-d1e8-4108-a3ac-a2183697232a\",\"X-Project-ID\":"
+ "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}";
remote.sendText(claimCreateMsg);
}
}

View File

@ -0,0 +1,45 @@
/*
* 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
*
* http://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.
*/
import java.io.IOException;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
@ClientEndpoint
public final class SampleZaqarEndpoint {
@OnOpen
public void onOpen(final Session sess) throws IOException {
final RemoteEndpoint.Basic remote = sess.getBasicRemote();
final String authenticateMsg = "{\"action\":\"authenticate\","
+ "\"headers\":{\"X-Auth-Token\":"
+ "\"8444886dd9b04a1b87ddb502b508261c\",\"X-Project-ID\":"
+ "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; // refer to bug
// #1553398
remote.sendText(authenticateMsg);
final String messagePostMsg = "{\"action\":\"message_post\",\"body\":"
+ "{\"messages\":[{\"body\":\"Zaqar Sample\"}],\"queue_name\":"
+ "\"SampleQueue\"},\"headers\":{\"Client-ID\":"
+ "\"355186cd-d1e8-4108-a3ac-a2183697232a\",\"X-Project-ID\":"
+ "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}";
remote.sendText(messagePostMsg);
}
}

View File

@ -0,0 +1,32 @@
/*
* 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
*
* http://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.
*/
const ws = new WebSocket('ws://localhost:9000');
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.body.messages)
console.log(msg.body.messages[0].body);
};
ws.onopen = () => {
ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \
"8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398
ws.send('{"action": "claim_create", "body": {"queue_name": "SampleQueue"}, \
"headers": {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", \
"X-Project-ID": "7530fad032ca431e9dc8ed4a5de5d99c"}}');
};

View File

@ -0,0 +1,25 @@
/*
* 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
*
* http://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.
*/
const ws = new WebSocket('ws://localhost:9000');
ws.onopen = () => {
ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \
"8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398
ws.send('{"action": "message_post", "body": {"messages": [{"body": \
"Zaqar Sample"}], "queue_name": "SampleQueue"}, "headers": \
{"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}');
};

View File

@ -0,0 +1,55 @@
/*
* 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
*
* http://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.
*/
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
@SuppressWarnings("serial")
@WebServlet(name = "SampleServlet", value = "/")
public final class SampleZaqarServlet extends HttpServlet {
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
final Client client = ClientBuilder.newClient();
final MultivaluedMap<String, Object> headers =
new MultivaluedHashMap<String, Object>();
headers.putSingle("Client-ID", "355186cd-d1e8-4108-a3ac-a2183697232a");
headers.putSingle("X-Auth-Token", "8444886dd9b04a1b87ddb502b508261c");
headers.putSingle("X-Project-Id", "7530fad032ca431e9dc8ed4a5de5d99c");
final Response res = client
.target("http://localhost:8888/v2/queues/SampleQueue/claims")
.request().headers(headers).post(Entity.json(""));
resp.getWriter().println(res.readEntity(String.class));
client.close();
}
}

View File

@ -0,0 +1,52 @@
/*
* 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
*
* http://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.
*/
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
@SuppressWarnings("serial")
@WebServlet(name = "SampleZaqarServlet", value = "/")
public final class SampleZaqarServlet extends HttpServlet {
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse resp) {
final Client client = ClientBuilder.newClient();
final MultivaluedMap<String, Object> headers =
new MultivaluedHashMap<String, Object>();
headers.putSingle("Client-ID", "355186cd-d1e8-4108-a3ac-a2183697232a");
headers.putSingle("X-Auth-Token", "8444886dd9b04a1b87ddb502b508261c");
headers.putSingle("X-Project-Id", "7530fad032ca431e9dc8ed4a5de5d99c");
client.target("http://localhost:8888/v2/queues/SampleQueue/messages")
.request(MediaType.APPLICATION_JSON_TYPE).headers(headers)
.post(Entity
.json("{\"messages\":[{\"body\":\"Zaqar Sample\"}]}"));
client.close();
}
}

View File

@ -0,0 +1,34 @@
/*
* 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
*
* http://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.
*/
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:9000');
ws.on('message', (data, flags) => {
const msg = JSON.parse(data);
if (msg.body.messages)
console.log(msg.body.messages[0].body);
});
ws.on('open', () => {
ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \
"8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398
ws.send('{"action": "claim_create", "body": {"queue_name": "SampleQueue"}, \
"headers": {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", \
"X-Project-ID": "7530fad032ca431e9dc8ed4a5de5d99c"}}');
});

View File

@ -0,0 +1,27 @@
/*
* 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
*
* http://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.
*/
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:9000');
ws.on('open', () => {
ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \
"8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398
ws.send('{"action": "message_post", "body": {"messages": [{"body": \
"Zaqar Sample"}], "queue_name": "SampleQueue"}, "headers": \
{"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", "X-Project-ID": \
"7530fad032ca431e9dc8ed4a5de5d99c"}}');
});

View File

@ -0,0 +1,30 @@
# 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
#
# http://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.
from zaqarclient.queues.v1 import client
client = client.Client('http://localhost:8888', conf={
'auth_opts': {
'options': {
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
'os_auth_url': 'http://localhost:5000/v3.0/',
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
}
}
}, version=2)
queue = client.queue('SampleQueue')
claim = queue.claim(ttl=600, grace=600) # refer to bug #1553387
for msg in claim:
print(msg)

View File

@ -0,0 +1,27 @@
# 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
#
# http://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.
from zaqarclient.queues.v1 import client
client = client.Client('http://localhost:8888', conf={
'auth_opts': {
'options': {
'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a',
'os_auth_token': '8444886dd9b04a1b87ddb502b508261c',
'os_auth_url': 'http://localhost:5000/v3.0/',
'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c'
}
}
}, version=2)
queue = client.queue('SampleQueue')
queue.post([{'body': 'Zaqar Sample'}])