Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Embedding Java code in a Jolie Service


Published on

Including existing Java code into a Jolie service is very easy. Follow these few slides for learning how to exploit all the Java code you have in a microservice architecture

Published in: Software
  • Be the first to comment

  • Be the first to like this

Embedding Java code in a Jolie Service

  1. 1. Including custom Java code in a Jolie service Copyright © 2014 The Jolie Team. This work is licensed under the Creative Commons Attribution License (CC BY). To view a copy of this license, visit
  2. 2. We want our Java code to be embedded in a Jolie service Here we have a very simple Java class with a method able to return the pow of two doubles. package org.jolie.sample; public class SampleClass { public double pow( double x, double y ) { return Math.pow( x, y ); } }
  3. 3. Add jolie.jar as a dependency to your project Before embedding your Java class in a Jolie service, you need to add the jolie.jar library as a dependency to your project. In Netbeans you need to add jolie.jar as an external library You can find jolie.jar in the folder where Jolie is installed.
  4. 4. Transform your class in a JavaService JavaService is the class provided with Jolie which enables you to import java methods of a class as if they were service operations package org.jolie.sample; import jolie.runtime.JavaService; public class SampleClass extends JavaService { public double pow( double x, double y ) { return Math.pow( x, y ); } }
  5. 5. The JavaService needs a Jolie interface Since the JavaService will be seen as a Jolie service, it needs to be described by a Jolie interface. You can write it in specific text file named SampleJavaServiceInterface.iol type PowRequest: void { .x: double .y: double } type PowResponse: void { .result:double } interface SampleJavaServiceInterface { RequestResponse: pow( PowRequest )( PowResponse ) } The name of the method is the name of the operation and the message types must specify input and output fields. In this case the input parameters are set in the field year of PowRequest, whereas the result is set in field result of PowResponse
  6. 6. The methods must exchange Jolie Values In order to exchange messages with a Jolie service we need to put input parameters and output results within a class called Value. package org.jolie.sample; import java.util.Calendar; import jolie.runtime.JavaService; import jolie.runtime.Value; public class SampleClass extends JavaService { public Value pow( Value request ) { double x = request.getFirstChild( "x" ).doubleValue(); double y = request.getFirstChild( "y" ).doubleValue(); double result = Math.pow( x, y ); Value response = Value.create(); response.getFirstChild( "result" ).setValue( result ); return response; } } Note that Value is a tree structured class which can be navigated using its methods. Here in particular, we exploit method getFirstChild for getting the first child node whose name is expressed within quotes. doubleValue returns the content of the node as a double, whereas the setValue allow for the assignment of a value into a node.
  7. 7. Prepare the jolie service Once the Java project is compiled, prepare the folder for your jolie service as it follows: lib SampleProject.jar SampleJavaServiceInterface.iol MyService.ol Put the jar file of your JavaService project into the folder lib This is the file of the interface of your JavaService The code of the service
  8. 8. The service code Now we can finish the the devlopment by writing the Jolie service which exploits the JavaService. include "SampleJavaServiceInterface.iol" include "console.iol" outputPort SampleJavaService { Interfaces: SampleJavaServiceInterface } embedded { Java: "org.jolie.sample.SampleClass" in SampleJavaService } main { request.x = 2.0; request.y = 3.0; pow@SampleJavaService( request )( response ); println@Console( response.result )() } Include the JavaService interface Bind the service to an outputPort. It is worth noting that no protocol and locations are needed Embed the JavaService into the jolie service The Java class path Binding with the outputPort Call the JavaService as it is a Jolie service
  9. 9. Try the demo jolie MyService.ol
  10. 10. The first language for microservices Copyright © 2014 The Jolie Team. This work is licensed under the Creative Commons Attribution License (CC BY). To view a copy of this license, visit