Software

How to Read an Attribute with the Jolokia REST API

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.core.Response;

@WebServlet(name = "SampleJolokiaServlet", value = "/")
public class SampleJolokiaServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    Response res = client.target("http://host:8161/jolokia/read/<MBean Name>/<Attribute Name>/<Inner Path>").request().get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}
Software

How to List MBeans with the Jolokia REST API

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.core.Response;

@WebServlet(name = "SampleJolokiaServlet", value = "/")
public class SampleJolokiaServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    Response res = client.target("http://host:8161/jolokia/list").request().get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}
Software

How to Receive a Message from an Apache ActiveMQ Topic with Java

JAX-RS

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.core.Response;

@WebServlet(name = "SampleActiveMQServlet", value = "/")
public class SampleActiveMQServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    Response res = client.target("http://host:8161/api/message/SampleTopic").request().get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

JMS

W/o Durability

import static java.lang.System.out;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQSample {

  public static void main(String[] args) throws Exception {
    ConnectionFactory connFactory = new ActiveMQConnectionFactory();

    Connection conn = connFactory.createConnection();

    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination dest = sess.createTopic("SampleTopic");

    MessageConsumer cons = sess.createConsumer(dest);

    conn.start();

    Message msg = cons.receive();

    out.println(msg);

    conn.close();
  }

}

W/ Durability

import static java.lang.System.out;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.Topic;

public class ActiveMQSample {

  public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ActiveMQConnectionFactory();

    Connection conn = factory.createConnection();

    conn.setClientID("SampleClient");

    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Topic dest = sess.createTopic("SampleTopic");

    MessageConsumer cons = sess.createDurableSubscriber(dest, "SampleSubscription");

    conn.start();

    Message msg = cons.receive();

    out.println(msg);

    conn.close();
  }

}

Java API for WebSocket

W/o Durability

import static java.lang.System.out;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint
public class SampleActiveMQEndpoint {

  @OnMessage
  public void onMessage(String msg) {

    if (msg.startsWith("MESSAGE"))
      out.println(msg);

  }

  @OnOpen
  public void onOpen(Session sess) throws IOException {
    RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("CONNECT\n\n\0");

    remote.sendText("SUBSCRIBE\ndestination:/topic/SampleTopic\n\nack:auto\n\n0");

    remote.sendText("DISCONNECT\n\n\0");
  }

}

W/ Durability

import static java.lang.System.out;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint
public class SampleActiveMQEndpoint {

  @OnMessage
  public void onMessage(String msg) {

    if (msg.startsWith("MESSAGE"))
      out.println(msg);

  }

  @OnOpen
  public void onOpen(Session sess) throws IOException {
    RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("CONNECT\nclient-id:SampleClient\n\n\0");

    remote.sendText("SUBSCRIBE\ndestination:/topic/SampleTopic\n\nack:auto\nactivemq.subscriptionName:SampleSubscription\n\n\0");

    remote.sendText("DISCONNECT\n\n\0");
  }

}
Software

How to Receive a Message from an Apache ActiveMQ Queue with Java

JAX-RS

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.core.Response;

@WebServlet(name = "SampleActiveMQServlet", value = "/")
public class SampleActiveMQServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    Response res = client.target("http://host:8161/api/message/SampleQueue").queryParam("type", "queue").request().get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

JMS

import static java.lang.System.out;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQSample {

  public static void main(String[] args) throws Exception {
    ConnectionFactory connFactory = new ActiveMQConnectionFactory();

    Connection conn = connFactory.createConnection();

    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination dest = sess.createQueue("SampleQueue");

    MessageConsumer cons = sess.createConsumer(dest);

    conn.start();

    Message msg = cons.receive();

    out.println(msg);

    conn.close();
  }

}

Java API for WebSocket

import static java.lang.System.out;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint
public class SampleActiveMQEndpoint {

  @OnMessage
  public void onMessage(String msg) {

    if (msg.startsWith("MESSAGE"))
      out.println(msg);

  }

  @OnOpen
  public void onOpen(Session sess) throws IOException {
    RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("CONNECT\n\n\0");

    remote.sendText("SUBSCRIBE\ndestination:SampleQueue\n\nack:auto\n\n0");

    remote.sendText("DISCONNECT\n\n\0");
  }

}
Software

How to Send a Message to an Apache ActiveMQ Topic with Java

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;

@WebServlet(name = "SampleActiveMQServlet", value = "/")
public class SampleActiveMQServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    Client client = ClientBuilder.newClient();

    client.target("http://host:8161/api/message/SampleTopic").queryParam("body", "Simples Assim").request(MediaType.TEXT_XML_TYPE).post(Entity.xml(""));

    client.close();
  }

}

JMS

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQSample {

  public static void main(String[] args) throws Exception {
    ConnectionFactory connFactory = new ActiveMQConnectionFactory();

    Connection conn = connFactory.createConnection();

    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination dest = sess.createTopic("SampleTopic");

    MessageProducer prod = sess.createProducer(dest);

    Message msg = sess.createTextMessage("Simples Assim");

    prod.send(msg);

    conn.close();
  }

}

Java API for WebSocket

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint
public class SampleActiveMQEndpoint {

  @OnOpen
  public void onOpen(Session sess) throws IOException {
    RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("CONNECT\n\n\0");

    remote.sendText("SEND\ndestination:/topic/SampleTopic\n\nSimples Assim\0");

    remote.sendText("DISCONNECT\n\n\0");
  }

}
Software

How to Send a Message to an Apache ActiveMQ Queue with Java

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;

@WebServlet(name = "SampleActiveMQServlet", value = "/")
public class SampleActiveMQServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    Client client = ClientBuilder.newClient();

    client.target("http://host:8161/api/message/SampleTopic").queryParam("type", "queue").queryParam("body", "Simples Assim").request(MediaType.TEXT_XML_TYPE).post(Entity.xml(""));

    client.close();
  }

}

JMS

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQSample {

  public static void main(String[] args) throws Exception {
    ConnectionFactory connFactory = new ActiveMQConnectionFactory();

    Connection conn = connFactory.createConnection();

    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination dest = sess.createQueue("SampleQueue");

    MessageProducer prod = sess.createProducer(dest);

    Message msg = sess.createTextMessage("Simples Assim");

    prod.send(msg);

    conn.close();
  }

}

Java API for WebSocket

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint
public class SampleActiveMQEndpoint {

  @OnOpen
  public void onOpen(Session sess) throws IOException {
    RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("CONNECT\n\n\0");

    remote.sendText("SEND\ndestination:SampleQueue\n\nSimples Assim\0");

    remote.sendText("DISCONNECT\n\n\0");
  }

}
Software

How to Delete a Cache with the Infinispan REST API

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 org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@WebServlet(name = "SampleInfinispanServlet", value = "/")
public class SampleInfinispanServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, HttpServletResponse resp) {
    Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("user", "password"));

    client.target("http://host:8080/rest/samplecache").request().delete();

    client.close();
  }

}
Software

How to Delete an Entry with the Infinispan REST API

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 org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@WebServlet(name = "SampleInfinispanServlet", value = "/")
public class SampleInfinispanServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, HttpServletResponse resp) {
    Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("user", "password"));

    client.target("http://host:8080/rest/samplecache/samplekey").request().delete();

    client.close();
  }

}
Software

How to Get an Entry with the Infinispan REST API

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.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@WebServlet(name = "SampleInfinispanServlet", value = "/")
public class SampleInfinispanServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("user", "password"));

    Response res = client.target("http://host:8080/rest/samplecache/samplekey").request().get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}
Software

How to Create an Entry with the Infinispan REST API

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 org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@WebServlet(name = "SampleInfinispanServlet", value = "/")
public class SampleInfinispanServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("user", "password"));

    Entity entity = ...

    client.target("http://host:8080/rest/samplecache/samplekey").request().put(entity); 

    client.close();
  }

}