How to Update 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("username", "password"));

    Entity entity = ...

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

    client.close();
  }

}

How to Search MBeans with the Jolokia REST API

W/ Configuration File

1) Create the SampleWorkItemHandler.java file:

import java.util.HashMap;
import java.util.Map;

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;

public class SampleWorkItemHandler implements WorkItemHandler {

  public void abortWorkItem(WorkItem item, WorkItemManager manager) {
    manager.abortWorkItem(item.getId());
  }

  public void executeWorkItem(WorkItem item, WorkItemManager manager) {
    Map<String, Object> resultMap = new HashMap<String, Object>();

    resultMap.put("name", "value");

    manager.completeWorkItem(item.getId(), resultMap);
  }

}

2) Create the /META-INF/SampleWorkItemHandlers.conf file:

[
  "Sample": new SampleWorkItemHandler() # incompatible with CDI
]

3) Create the /META-INF/drools.session.conf file:

drools.workItemHandlers=SampleWorkItemHandlers.conf

W/ KIE API

1) Create the SampleWorkItemHandler.java file:

import java.util.HashMap;
import java.util.Map;

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;

public class SampleWorkItemHandler implements WorkItemHandler {

  public void abortWorkItem(WorkItem item, WorkItemManager manager) {
    manager.abortWorkItem(item.getId());
  }

  public void executeWorkItem(WorkItem item, WorkItemManager manager) {
    Map<String, Object> resultMap = new HashMap<String, Object>();

    resultMap.put("name", "value");

    manager.completeWorkItem(item.getId(), resultMap);
  }

}

2) Create the SampleServlet.java file:

import java.util.HashMap;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.process.WorkItemManager;

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

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    RuntimeManager runtimeManager = ... // or inject it

    RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(...);

    KieSession sess = engine.getKieSession();

    WorkItemManager workItemManager = sess.getWorkItemManager();

    workItemManager.registerWorkItemHandler("Sample", new SampleWorkItemHandler()); // or inject it

    ...
  }

}

W/ Work Item Handler Producer

1) Create the SampleWorkItemHandler.java file:

import java.util.HashMap;
import java.util.Map;

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;

public class SampleWorkItemHandler implements WorkItemHandler {

  public void abortWorkItem(WorkItem item, WorkItemManager manager) {
    manager.abortWorkItem(item.getId());
  }

  public void executeWorkItem(WorkItem item, WorkItemManager manager) {
    Map<String, Object> resultMap = new HashMap<String, Object>();

    resultMap.put("name", "value");

    manager.completeWorkItem(item.getId(), resultMap);
  }

}

2) Create the SampleWorkItemHandlerProducer.java file:

import java.util.HashMap;
import java.util.Map;

import org.jbpm.runtime.manager.api.WorkItemHandlerProducer;
import org.kie.api.runtime.process.WorkItemHandler;

public class SampleWorkItemHandlerProducer implements WorkItemHandlerProducer {

  @Override
  public Map<String, WorkItemHandler> getWorkItemHandlers(String identifier, Map<String, Object> params) {
    Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();

    workItemHandlers.put("Sample", new SampleWorkItemHandler()); // inject if using CDI

    return workItemHandlers;
  }

}

3) Create the SampleServlet.java file:

import java.util.HashMap;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.runtime.manager.impl.cdi.InjectableRegisterableItemsFactory;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;
import org.kie.api.runtime.manager.RuntimeManager;

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

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    RuntimeEnvironment env = RuntimeEnvironmentBuilder.Factory.get()...registerableItemsFactory(new InjectableRegisterableItemsFactory()).get(); // or inject it

    RuntimeManager runtimeManager = ... // or inject it

    RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(...);

    KieSession sess = engine.getKieSession();

    ...
  }

}

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 final class SampleJolokiaServlet extends HttpServlet {

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

    final Response res = client.target("http://localhost:8161/jolokia/exec/<MBean Name>/<Operation Name>/<Arg>/<Arg2>/...").request().get();

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

    client.close();
  }

}

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 final class SampleJolokiaServlet extends HttpServlet {

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

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

    client.close();
  }

}

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 final class SampleJolokiaServlet extends HttpServlet {

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

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

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

    client.close();
  }

}

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 final class SampleJolokiaServlet extends HttpServlet {

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

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

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

    client.close();
  }

}

How to Receive a Message from an Apache ActiveMQ Topic with 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 final class SampleActiveMQServlet extends HttpServlet {

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

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

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

    client.close();
  }

}

How to Receive a Message from an Apache ActiveMQ Queue with 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 final class SampleActiveMQServlet extends HttpServlet {

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

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

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

    client.close();
  }

}

How to Send a Message to an Apache ActiveMQ Topic with 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 final class SampleActiveMQServlet extends HttpServlet {

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

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

    client.close();
  }

}

How to Send a Message to an Apache ActiveMQ Queue with 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 final class SampleActiveMQServlet extends HttpServlet {

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

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

    client.close();
  }

}