Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Using JBoss DataGrid in Openshift PaaS

 

April 28, 2017
Francesco Marchioni
Related topics:
Kubernetes
Related products:
Red Hat Data GridRed Hat JBoss Enterprise Application PlatformRed Hat OpenShift Container Platform

Share:

    This article describes how to run a client-server application for JBoss Data Grid on Openshift using Red Hat Container Development Kit 3.0 Beta and Minishift. This environment for this tutorial can be set up quickly following up this previous post on the Developer Blog.

    First of all, you need to make sure you have available the ImageStreams and Templates, in order to run JBoss Data Grid in your Openshift Paas. You can check that your environment contains both of them through the oc command as follows:

    $ oc get is -n openshift | grep datagrid
    jboss-datagrid65-openshift            registry.access.redhat.com/jboss-datagrid-6/datagrid65-openshift               1.2,1.2-13,1.2-18 + 2 more...      2 weeks ago
    
    $ oc get templates -n openshift | grep datagrid
    datagrid65-basic                    Application template for JDG 6.5 applications.

    If you don’t have ImageStreams and Templates loaded yet then you can quickly download them from the official repository available at https://github.com/openshift/library/tree/master/official/datagrid.

    Now, you can create both resources as follows:

    # create imagestream
    $ oc create -f Jboss-datagrid65-openshift-rhel7.json
    # create template
    $ oc create -f datagrid65-basic.json

    Ok, now we’re ready to rock. As an example, we will create a new project named “jdg-demo” which will contain the JBoss DataGrid Service, which will be invoked from a client application running in an EAP 6 Service.

    By the way, as mentioned from the Documentation, at the moment Client-Server mode is the only native option to access JBoss Data Grid in a PaaS since Library Mode is not supported.

    In order to create your project, enter the following commands from a shell:

    $ oc new-project jdg-demo
    Now using project "jdg-demo" on server "https://192.168.42.149:8443"

    The New step will be adding a new application to it, using the datagrid65-basic template:

    $ oc new-app datagrid65-basic --name datagrid
    Deploying template "datagrid65-basic" in project "openshift"
    
    datagrid65-basic
    ---------
    Application template for JDG 6.5 applications.
    
    * With parameters:
    * APPLICATION_NAME=datagrid-app
    * HOSTNAME_HTTP=
    * USERNAME=
    * PASSWORD=
    * IMAGE_STREAM_NAMESPACE=openshift
    * INFINISPAN_CONNECTORS=hotrod,memcached,rest
    * CACHE_NAMES=
    * ENCRYPTION_REQUIRE_SSL_CLIENT_AUTH=
    * MEMCACHED_CACHE=default
    * REST_SECURITY_DOMAIN=
    * JGROUPS_CLUSTER_PASSWORD=sKPBot52 # generated
    
    Creating resources with label app=datagrid ...
    service "datagrid-app" created
    service "datagrid-app-memcached" created
    service "datagrid-app-hotrod" created
    route "datagrid-app" created
    deploymentconfig "datagrid-app" created
    Success
    Run 'oc status' to view your app.

    As you could see from the output, a set of services has been created in order to expose the service to the cluster. In particular, we’re interested in the service "datagrid-app-hotrod"  which will be contacted by our EAP client application. To confirm our services are ok, we will execute the following check:

    $ oc get svc
    NAME                     CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
    datagrid-app             172.30.139.80    <none>        8080/TCP    15s
    datagrid-app-hotrod      172.30.10.134    <none>        11333/TCP   15s
    datagrid-app-memcached   172.30.198.163   <none>        11211/TCP   15s

    Ok, we’re almost done. Although we will not explore cluster capabilities in this article, in order to use them the Kubernetes' PING protocol, which is used to look up other nodes in the Infinispan cluster, needs to be enabled. This can be done using the default Service Account as follows:

    $ oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)

    We are done with JBoss data Grid, and our Web console should reflect the status of your available services and route:

    Now let’s add another application, which is based on the jboss-eap64 template and does S2I of a simple web application hosted at https://github.com/fmarchioni/ in the Web Context eap-hotrod:

    $ oc new-app jboss-eap64-openshift~https://github.com/fmarchioni/ocpdemos --name eap-hotrod --context-dir=eap-hotrod -e HOTROD_SERVICE=datagrid-app-hotrod -e HOTROD_SERVICE_PORT=11333
    
    Creating resources with label app=eap-hotrod ...
    imagestream "eap-hotrod" created
    buildconfig "eap-hotrod" created
    deploymentconfig "eap-hotrod" created
    service "eap-hotrod" created
    Success
    Build scheduled, use 'oc logs -f bc/eap-hotrod' to track its progress.
    Run 'oc status' to view your app.

    As you could see, we have passed some environment variables to our application. These variables are used to solve the name of JDG Hot Rod service and its port. As you can see from the minimal Servlet contained in it, we abstract from the Hostname and Port where the Hot Rod service is running and just use the name of Openshift Service (and its port) passed as Environment variable:

    package infinispan;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    
    import java.util.UUID;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.infinispan.client.hotrod.RemoteCache;
    import org.infinispan.client.hotrod.RemoteCacheManager;
    import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
    
    @SuppressWarnings("serial")
    @WebServlet(value = "/test")
    public class TestServlet extends HttpServlet {
    
     private RemoteCacheManager cacheManager;
     private RemoteCache < String, Object > cache;
    
     @Override
     public void init() {
    
      ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.addServer().host(System.getenv("HOTROD_SERVICE"))
       .port(Integer.parseInt(System.getenv("HOTROD_SERVICE_PORT")));
      cacheManager = new RemoteCacheManager(builder.build());
      cache = cacheManager.getCache("default");
    
     }
    
     protected void doGet(HttpServletRequest req, HttpServletResponse res) {
      doPost(req, res);
     }
    
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse res) {
    
      res.setContentType("text/html");
    
      String name = req.getParameter("name");
      String surname = req.getParameter("surname");
      String teamName = req.getParameter("teamname");
    
    
      PrintWriter out = null;
      try {
       out = res.getWriter();
    
       Player player = new Player();
       player.setName(name);
       player.setSurname(surname);
       player.setTeamName(teamName);
       String randomId = UUID.randomUUID().toString();
    
       cache.put(randomId, player);
    
       out.println("Added Player: " + cache.get(randomId));
    
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
    
     }
    }

    You can complete the example by exposing the EAP route as follows:

    $ oc expose service eap-hotrod
    route "eap-hotrod" exposed

    In a couple of minutes, your service should be available as you can see from the Web console:

    Clicking on the Service’s route, you will be redirected to the welcome page, which is a simple JSP frontend to the Servlet. Add some data in it to test it:

    Finally, when you are done with experimenting, you can remove everything using 'oc delete' command:

    $ oc delete all -l "app=eap-hotrod"
    $ oc delete all -l "app=datagrid"

    About the Author:

    Francesco Marchioni is a Senior Technical Account Manager on Middleware products based in Rome (Italy). Some of its contributions to the Community of JBoss developers are available on the site http://www.mastertheboss.com


    Click here to download JBoss Data Grid.

    Last updated: July 27, 2023

    Recent Posts

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    What’s up next?

     

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue