Tuesday, October 4, 2016

Build MSF4J with WSO2 Carbon Kernel 5.1.0

MSF4J supports standalone mode as well as the OSGi mode. Unlike other WSO2 products, MSF4J is not released as a typical server. But you can build up your own server by installing the MSF4J feature in to the Carbon Kernel.
You can used the pom based approach to install the MSF4J feature into the Kernel 5.1.0

Steps

Copy the following pom.xml and carbon.product file to a some directory
Run “mvn clean install” inside the directory.
If the build is success, you can find the build product inside the target folder. You can run the server by executing the wso2carbon-kernel-5.1.0/bin/carbon.sh file in the target directory.

Add a Microservice to the Server

You can build the stockquote bundle sample as mentioned inhttps://github.com/wso2/msf4j/tree/master/samples/stockquote/bundleThen copy the bundle to wso2carbon-kernel-5.1.0/osgi/dropins directory and start the server.
To invoke the deployed service use below curl command
curl http://localhost:9090/stockquote/IBM

MSF4J will Support JAX-RS Sub-Resource and Running Multiple Microservice Runners


WSO2 MSF4J will will support JAX-RS sub resources and running multiple microservice runners in its’ next release along with some great features.

JAX-RS Sub-Resource

You can try out the sub-resources with the latest MSF4J snapshot version. https://github.com/wso2/msf4j/tree/master/samples/subresource is the MSF4J Sub resource sample that demonstrate how to use sub-resource with MSF4J.

Running Multiple MicroservicesRunners

In previous MSF4J versions we only support running a single MicroservicesRunner in a single JVM. With the next release you can run several MicroservicesRunners in a single JVM.
Following code segment shows a simple sample of the usage.
Tryout the new features in the MSF4J and provide your feedback. Also please do raise issues in JIRA/Github if you encounter anything.

Friday, July 22, 2016

WSO2 Microservices Framework for Java (WSO2 MSF4J) 2.0.0 Released

We are pleased to announce the release of WSO2 Microservices Framework for Java (WSO2 MSF4J) 2.0.0. The distribution, source and tag location for this release is available here.
WSO2 MSF4J is a lightweight, high performance microservices runtime. It also provides a very simple programming model for developing Java based microservices.
This release of the WSO2 MSF4J includes the following key features. For further details please refer to the Product Page.

What’s New in This Release

  • Spring support MSF4J now supports Spring annotations and runtime
  • Swagger support 
    Generate Swagger definitions for your microservices and annotate services using Swagger annotations
  • Exception mapper supportAllows mapping of Java exceptions to custom HTTP responses
  • Streaming output support 
    Provides full control over streaming to the service developer
  • FormParam and FormDataParam annotation support 
    Provides support to handle x-form-application-urlencoded and multipart/form-data requests with form submission
  • FormParamIterator support
    Provides full control to the developer over form field processing
  • Circuit breaker supportNygard’s circuit breaker pattern is supported via Netflix Hystrix
  • Improved thread model and transport architecture
    Netty and execution thread pools can now be customized according to the required service characteristics
In addition to the above features, MSF4J supports analysis with visualization with WSO2 Data Analytics Server (DAS), and includes a set of comprehensive samples which demonstrate how to use MSF4J features & build Microservices Architecture (MSA) based solutions. This release also includes several bug fixes to the previous version of MSF4J. To find out more, visit the MSF4J product page.
How you can contribute
We welcome contributors: head over to wso2.com/community to find out more. If you have any suggestions or are interested in WSO2 MSF4J 2.0.0 discussions, please join the dev@wso2.org or architecture@wso2.org mailing lists.
Reporting Issues
Please report issues and documentation errors regarding WSO2 MSF4J 2.0.0 through the public issue tracking system

Sunday, July 10, 2016

Face Recognition in Video with JavaCV

Few weeks ago some random guy asked me whether I can help him to do face recognition in a video. At that time I didn’t had much free time so couldn’t look much into that. In this weekend suddenly I recall that and wanted to check on that.
There can be lot of improvements to be done for this. But for someone who just wanna give a shot here is the code

Wednesday, June 15, 2016

FormParam with MSF4J

In JAX-RS, we can use @FormParam annotation to bind HTML form parameters value to a Java method.
Next MSF4J supports below content types for FormParam
  1. application/x-www-form-urlencoded
  2. multipart/form-data


For the application/x-www-form-urlencoded
This is pretty much similar to the QueryParam. MSF4J reads the Request body and get the decode the encoded values and pass the values to the service.


E.g.
Sample service for application/x-www-form-urlencoded content type
@POST
@Path("/formParam")
public Response testFormParam(@FormParam("age") int age, @FormParam("name") String name) {
  System.out.println("Name " + name);
  System.out.println("Age" + age);
  return Response.ok().entity("Name and age " + name + ", " + age).build();
}


For the multipart/form-data
@POST
@Path("/formParam")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA})
public Response testFormParam(@FormParam("age") int age, @FormParam("name") String name) {
  System.out.println("Name " + name);
  System.out.println("Age" + age);
  return Response.ok().entity("Name and age " + name + ", " + age).build();
}


If a user wants, MSF4J will not process the stream but rather directly pass an FormParamIterator which can be used to retrieve the parameter values.
FormParam name must be ‘form’ and the type must be FormParamIterator


E.g.
Sample service for multipart/form-data content-type with file upload
@POST
@Path("/simpleFormStreaming")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response simpleFormStreaming(@Context FormParamIterator formParamIterator) {
  try {
      while (formParamIterator.hasNext()) {
          FormItem item = formParamIterator.next();
          if (item.isFormField()) {
              System.out.println(item.getFieldName() + " - " + StreamUtil.asString(item.openStream()));
          } else {

           Files.copy(file.toPath(), Paths.get(System.getProperty("java.io.tmpdir"), file.getName()));           
}

      }
  } catch (FileUploadException e) {
      log.error("Error while uploading the file " + e.getMessage(), e);
  } catch (IOException e) {
      log.error("Unable to upload the file " + e.getMessage(), e);
  }
  return Response.ok().entity("Request completed").build();
}


If you like use non streaming mode then you can directly get File objects in a file upload. Here rather than @FormParam you need to use @FormDataParam annotation. This annotation can be used with all FormParam supported data types plus File and bean types as well as with InpuStreams.


If you wanna upload set of files. Then a sample service would be like as follows
@POST
@Path("/multipleFiles")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response multipleFiles(@FormDataParam("files") List<File> files) {
  files.forEach(file -> {
      try {
          Files.copy(file.toPath(), Paths.get("/tmp", "tst", file.getName()));
      } catch (IOException e) {
          log.error("Error while Copying the file " + e.getMessage(), e);
      }
  });
  return Response.ok().entity("Request completed").build();
}


You can use more complex times with combination of primitives, beans and files as follows
@POST
@Path("/complexForm")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response complexForm(@FormDataParam("file") File file,
                      @FormDataParam("id") int id,
                      @FormDataParam("people") List<Person> personList,
                      @FormDataParam("company") Company animal) {
  System.out.println("First Person in List " + personList.get(0).getName());
  System.out.println("Id " + id);
  System.out.println("Company " + animal.getType());
  try {
          Files.copy(file.toPath(), Paths.get(System.getProperty("java.io.tmpdir"), file.getName()));
  } catch (IOException e) {
      log.error("Error while Copying the file " + e.getMessage(), e);
  }
  return Response.ok().entity("Request completed").build();
}

If you like to get the InputStream of a file then you can go ahead like below example. There FileInfo bean will hold the filename and the content type attributes of the particular inputstream. Note that attribute names must be equal when you use InpuStream. Here I’ve used ‘file’ for the both params.
@POST
@Path("/streamFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response multipleFiles(@FormDataParam("file") FileInfo fileInfo,
                            @FormDataParam("file") InputStream inputStream) {
  try {
      Files.copy(inputStream, Paths.get(System.getProperty("java.io.tmpdir"), fileInfo.getFileName()));
  } catch (IOException e) {
      log.error("Error while Copying the file " + e.getMessage(), e);
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
  } finally {
      IOUtils.closeQuietly(inputStream);
  }
  return Response.ok().entity("Request completed").build();
}

Tryout with latest msf4j
You can try out these scenarios with the latest msf4j snapshot. In order to do so get the msf4j source from the https://github.com/wso2/msf4j and build it with maven.