How to Send a Message to OpenStack Zaqar

JAX-RS

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, String> headers = new MultivaluedHashMap<>();

    headers.putSingle("Client-ID", ...);

    headers.putSingle("X-Auth-Token", ...);

    headers.putSingle("X-Project-Id", ...);

    client.target("http://host:8888/v1.1/queues/SampleQueue/messages").request(MediaType.APPLICATION_JSON_TYPE).headers(headers).post(Entity.json("{\"messages\":[{\"body\":\"Simples Assim\"}]}"));

    client.close();
  }
 
}

Java API for WebSocket

SampleZaqarEndpoint.java

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();

    remote.sendText("{\"action\":\"authenticate\",\"headers\":{\"X-Auth-Token\":...,\"X-Project-ID\":...}}"); // refer to bug #1553398

    remote.sendText("{\"action\":\"message_post\",\"body\":{\"messages\":[{\"body\":\"Simples Assim\"}],\"queue_name\":\"SampleQueue\"},\"headers\":{\"Client-ID\":...,\"X-Project-ID\":...}}");
  }

}

SampleZaqarServlet.java

import java.net.URI;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;

@SuppressWarnings("serial")
@WebServlet(name = "SampleZaqarServlet", value = "/")
public final class SampleZaqarServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    container.connectToServer(SampleZaqarEndpoint.class, new URI("ws://host:9090"));
  }

}

JavaScript

const ws = new WebSocket('ws://host:9000');
 
ws.onopen = () => {
  ws.send('{"action":"authenticate","headers":{"X-Auth-Token":...,"X-Project-ID":...}}'); // refer to bug #1553398

  ws.send('{"action":"message_post","body":{"messages":[{"body":"Simples Assim"}],"queue_name":"SampleQueue"},"headers":{"Client-ID":...,"X-Project-ID":...}}');
};

Node.js

const WebSocket = require('ws');

const ws = new WebSocket('ws://host:9000');

ws.on('open', () => {
  ws.send('{"action":"authenticate","headers":{"X-Auth-Token":...,"X-Project-ID":...}}'); // refer to bug #1553398
 
  ws.send('{"action":"message_post","body":{"messages":[{"body":"Simples Assim"}],"queue_name":"SampleQueue"},"headers":{"Client-ID":...,"X-Project-ID":...}}');
});

Python

from zaqarclient.queues.v1 import client

client = client.Client('http://host:8888', conf={'auth_opts':{'options':{'client_uuid':..., 'os_auth_token':..., 'os_auth_url':'http://host:5000/v3.0/', 'os_project_id':...}}}, version=1.1)

queue = client.queue('SampleQueue')

queue.post([{'body':'Simples Assim'}])
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s