Software

How to Search 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;

@SuppressWarnings("serial")
@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://localhost:8161/jolokia/search/<Pattern>").request().get();

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

    client.close();
  }

}
Software

How to Execute an Operation 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/exec/<MBean Name>/<Operation Name>/<Arg>/<Arg2>/...").request().get();

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

    client.close();
  }

}
Software

How to Write an Attribute with the Jolokia 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;

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

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

    client.target("http://host:8161/jolokia/write/<MBean Name>/<Attribute Name>/<Attribute Value>/<Inner Path>").request().get();

    client.close();
  }

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

}