Archive

Archive for the ‘Java9’ Category

Java9 : ServiceLoader

java.util.ServiceLoader class loads the service providers/implementations at run time. While compile time ServiceLoader just need to know Service interface. With the Java9 modularity, we can add service implementation modules dynamically at run time, And Application can have new implementation with out effecting anything,

lets check with an example, create a module EventsAPI with an interface  EventService.java

EventsAPI/EventService.java


package events.api;

public interface EventService {
 public String getName();
}

EventsAPI/module-info.java


module events.api {
 exports events.api;
}

Create an implementation to the EventService interface in another module called FacebookEvents

FacebookEvents/FacebookEventService.java

package events.api.facebook;
import events.api.EventService;

public class FacebookEventService implements EventService{

 public FacebookEventService() {
     System.out.println("FacebookEventService Constructor");
 }
 public String getName() {
    return "facebook events";
 }
}

 

FacebookEvents/module-info.java

module events.api.facebook {
 requires events.api;

 provides events.api.EventService
 with events.api.facebook.FacebookEventService;
}

 

FacebookEvents module requires EventsAPI, because it need to have access to EventService.java interface.

And it provides EventService implementation with FacebookEventService.

Lets create a Client module EventsClient to consume EventsAPI

EventsClient/module-info.java

 

module client.calendar {
requires events.api;
uses events.api.EventService;
}

We are going to use ServiceLoader to find the implementations of EventService interface, here ServiceLoader requires uses keyword on EventService, otherwise compiler will throw an error.

Finally Client Test class

EventsClient/Calendar.java

package client.calendar;
import java.util.ServiceLoader;
import events.api.EventService;

public class Calendar {
public static void main(String[] args) {
System.out.println("Calendar events..!!!");
ServiceLoader<EventService> events = ServiceLoader.load(EventService.class);

for(EventService event : events) {
System.out.println(event.hashCode() + " : " +event.getName());
}
events.reload();
for(EventService event : events) {
System.out.println(event.hashCode() + " : " +event.getName());
}
}
}

In the EventsClient module, we din’t mention anything about FacebookEvents module, while running the above Calendar.java add FacebookEvents module, the output will be

output

Calendar events..!!!
FacebookEventService Constructor
1627960023 : facebook events
FacebookEventService Constructor
745160567 : facebook events

ServiceLoader found the EventService implementation FacebookEventService and showed the output, lets add another implementation to the EventService interface and examine the output from above client

TwitterEvents/module-info.java


module events.api.twitter {
requires events.api;
provides events.api.EventService
with events.api.twitter.TwitterEventService;
}

same as FacebookEventService, will have TwitterEventService which will implement EventService interface

TwitterEvents/TwitterEventService.java


package events.api.twitter;

import events.api.EventService;

public class TwitterEventService implements EventService{

public TwitterEventService() {
System.out.println("TwitterEventService Constructor");
}

public String getName() {
return "twitter events";
}
}

Run the EventsClient/Calendar.java by adding TwitterEvents module on the modulepath, output as follows

Calendar events..!!!
TwitterEventService Constructor
249515771 : twitter events
FacebookEventService Constructor
1627960023 : facebook events
TwitterEventService Constructor
321142942 : twitter events
FacebookEventService Constructor
745160567 : facebook events

We have just added TwitterEvents module in the run time, ServiceLoader is able to load the TwitterEventService and gave the desired output.

Source code is available at https://github.com/rameshcharykotha/java9practice

Thanks for reading..!!

Categories: Java, Java9

Java9: Welcome to Module world

Java 9 has been released on September 21 officially, Eclipse is supporting Java 9 from  Eclipse Oxygen.1a (4.7.1a), Lets jump into module world..!!!

Download Java 9 from here, and add it to Eclipse Installed JRE’s as below

Eclipse - JRE

That’s it, we are good to write Java 9 module programs in Eclipse.

  1. Create First java project and add module-info.java to it, right click on the project java9-module-info

module-info.java

module first {

}

Module should start with keyword module followed by its name. currently it doesn’t requires anything or it doesn’t export anything.

2. Let’s create Second java project,

module-info.java

module second {
exports second; --<em> second module is exporting a package "second"</em>
}

Second.java — create a simple Java class with public sayHello() method in second package

package second;

public class Second {
public void sayHello() {
System.out.println("Welcome to module world..!!");
}
}

Second java class will be available to other modules, as it is exporting second package.

3. Update first module –  module-info.java as below

module first {
requires second;
}

Now first module requires second module, but it will fail with module can not be resolved compilation error

“second cannot be resolved to a module”

we need to add second module as dependency to first module.

module-path

Check the new thing called modulepath

3. Create FirstTest.java in first package as below

package first;

import second.Second; // we are accessing Second.java from second module

public class FirstTest {

public static void main(String[] args) {
Second second = new Second();
second.sayHello();
}
}

Running the above would print “Welcome to module world..!!

source code can be found at Github

Categories: Java, Java9