The EJB Project - TemperatureConverterEJB

The Java EJB project that contains a stateless EJB 3.0 Java class. It uses the RunUnit class to wrap around the JVM COBOL code.

The following steps introduce you to the features that enable you to deploy this project alongside the other two projects.

  1. In the Project Explorer pane, navigate to TemperatureConverterEJB > ejbModule > com.microfocus.businesslogic and double-click TemperatureConverterBean.java.

    This opens the Java class in an editor.

    This is the EJB class, as denoted by the @Stateless annotation. It imports the com.microfocus.cobol.runtimeservices API, which provides the Micro Focus RunUnit class. The following code shows a TemperatureConverter instance being wrapped in a RunUnit. Each instantiated RunUnit contains its own resources that enable it to run independent of other RunUnit instances, thus enabling the application to provide a multi-user environment.

    import com.microfocus.cobol.runtimeservices.*;
    ...
    private TemperatureConverter createTemperatureConverter()
     {
      rununit = new RunUnit();
      TemperatureConverter tempConverter = new TemperatureConverter();
      rununit.Add(tempConverter);
      return tempConverter;
     }
    The RunUnit syntax includes a try...finally block containing a call to the StopRun method, ensuring that the resources used by the JVM COBOL are freed up when required, even in the event of an exception.
    ...
    TemperatureConverter temperatureConverter = createTemperatureConverter();		
     try
     {			
    	 c  = temperatureConverter.toCelsius(f);
     }
     finally
     {		
      rununit.StopRun();
     }
    ...		
  2. In the Project Explorer pane, navigate to TemperatureConverterEJB > ejbModule > com.microfocus.business and double-click ITemperatureConverter.java.

    The interface implemented by the previous class appears.

    The interface imports the JAX-WS-RS API to create a RESTful application. This can be seen by the annotations (@GET, @Path, @Produces) used throughout the TemperatureConverterBean class. For example, the following toCelsius method uses the annotations to configure how the request is handled:

    getCelsius method

    The annotations help to build the URL that forms the request in the deployed application. The return phrase indicates that the response to the client will be returned in JSON format using a simple Temperature class.

  3. In the Project Explorer pane, navigate to TemperatureConverterEJB > ejbModule > com.microfocus.businesslogic and double-click Temperature.java.

    The class is annotated with @XmlRootElement (name = "Temperature" ), which allows the build() API to use reflection on the class to build up the JSON output based on the fields and their values. The build() method then adds the appropriate header information to pass back to the client.

  4. In the Project Explorer pane, navigate to TemperatureConverterEJB > ejbModule > com.microfocus.businesslogic and double-click TemperatureConverterApp.java.

    This is the wrapper class that enables the EJB to be deployed to the application server.

    @ApplicationPath("/")
    public class TemperatureConverterApp extends javax.ws.rs.core.Application {
    @Override
     public Set<Class<?>> getClasses(){
      Set<Class<?>> s = new HashSet<Class<?>>();
      s.add(TemperatureConverterBean.class);
      return s;
     }
    }

    By extending the JAX-WS-RS API this time, it uses the getClasses() method to register the TemperatureConverterBean EJB class with the application server, as a REST Web service.

  5. In the Project Explorer pane, navigate to TemperatureConverterEJB > resource.

    As with the JVM COBOL project, this project contains a cobconfig.properties file configured in the same way. If one does not exist, use the File > New > File options to add a new resource file.

  6. You must ensure that cobconfig.properties is deployed as part of the project:
    1. Right-click TemperatureConverterEJB, then choose Properties.
    2. Choose Deployment Assembly.

      The cobconfig.properties file is in the resource directory, and so is set to be included in the assembly when it is deployed.


      Deployment Assembly
    3. Click Cancel to close the Properties page.