Pages

Showing posts with label IntelliJ IDEA. Show all posts
Showing posts with label IntelliJ IDEA. Show all posts

Friday, June 24, 2016

Robo4j log stardate 08242.042:: 1st JavaFx Joystick demo taste

   Around one week ago has been successfully released robo4j version alfa-0.2. On the robo4j.io web are touched main features which are inside. Since that moment Robo4j went through continual strong development. 
  New agents, BUSes has been implemented to push Robo4j realtime performance forward. 
The Robo4j is moving to achieve the goal. The goal to be intelligent autonomous multi-agents system which is capable to re-configure him self to achieve the goal. 
  Now is coming small taste of Robo4j demo (JavaFX Joystick). It means Robo4j framework has been used to in other independent app development. 
  Enjoy and new blog posts are coming soon. 


Saturday, May 28, 2016

DukeScript marries IntelliJ IDEA


Few days/weeks ago I've been volunteering on making DukeScript working together with IntelliJ IDEA. My friend has written very nice words about the result of our work. 

Enjoy!

DukeScript for IntelliJ IDEA


Saturday, September 6, 2014

HowTo:: Threading: Producer Consumer with thread-safe PriorityBlockingQueue as buffer with custom ordering


Img.1.:: Producer Consumer Problem examples (powered by IntelliJ Idea 13.x)

  In the previous blog post I've blogged about commonly issue in multi-threading Producer-Consumer problem with shared not synchronised buffer. Buffer also showed how to employ some conditions to control the access to it. (link)
  This ConsumerProducer example is more sophisticated. In this example is used MathPriorityTransferQueue which extends PriorityBlockingQueue and implements TransferQueue to employ better control over the exchange process itself. 

// methods to @Override from interface TransferQueue< e >
boolean tryTransfer(E e){...
boolean tryTransfer(E e, long timeout, TimeUnit unit){...
void transfer(E e) throws InterruptedException {...
boolean hasWaitingConsumer(){...
int getWaitingConsumerCount(){..

// methods to @Override from class PriorityBlockingQueue
public E take() throws InterruptedException{...
public E peek(){...
  The Distribution Systems, Parallel computing, machine learning or artificial intelligence are always excited, this is only small introduction to some small part that is worthy to know to make them possible,  so let's  start slowly with the example. 

   The heart of the example is the implementation of MathPriorityTransferQueue which is the buffer used by MathProducers and MathConsumers. The buffer allows them to work together. The basic idea of the example is that each PRODUCER generates NUMBER_PRODUCER_EVENTs that number  of consumers (NUMBER_CONSUMER) process. The total number of MathEvents that CONSUMERs needs to consume is then number of producers multiplied by the number of MathEvents they generates. 
Each MathEvent has assigned the random priority. The Priority is the key how the event are sorted inside MathPriorityTransferQueue.
The main class MathPrioProdConMain takes responsibility about the process of Producers creation and number of consumers. Important is to point out that Consumers will work only if something is available to consume inside the queue (simply when data are available). 
public class MathPrioProdConMain {
    ...
    private static final int NUMBER_CONSUMER = 2;
    private static final int NUMBER_PRODUCERS= 10;
    private static final int NUMBER_PRODUCER_EVENTS= 1000;

    public static void main(String... args){
        MathPriorityTransferQueue< MathEvent > buffer = new MathPriorityTransferQueue<>();
        ...
        Thread[] producerThread = new Thread[ NUMBER_PRODUCERS ];
        for(int i=0; i < NUMBER_PRODUCERS; i++){
            producerThread[i] = new Thread(new MathProducer(NUMBER_PRODUCER_EVENTS, buffer));
            producerThread[i].start();
        }
        ...
        Thread[] consumerThread = new Thread[ NUMBER_CONSUMER ];
        for(int i=0; i < NUMBER_CONSUMER; i++){
            consumerThread[i] = new Thread(new MathConsumer(buffer));
            consumerThread[i].start();
        }
        ...
    }
}
  To test the ordering abilities of the shared data-structure the main method sends unexpected MathEvents into the buffer with defined priority. On the output shows that the ordering is implemented correctly.
...
MathEvent event = new MathEvent(" Transfer Math SuperEvent ", 22);
buffer.transfer( event );
...
  At the end we wait until all consumer processes are done (all threads simply die). 

...
for(int i=0; i < NUMBER_CONSUMER ; i++)
                consumerThread[ i ].join();
...>
  We may keep in mind that the whole multi-threading process  works like state machine with its locking, un-locking, notifying and operations synchronisation which helps to understand why the rest of the code works.  


  Before we move into the code of MathPriorityTransferQueue we take a brief look what individual MathProducer is doing: 
public class MathProducer implements Runnable{
    ...
    public MathProducer(int maxMathEvents, MathPriorityTransferQueue< MathEvent > buffer){
        this.maxMathEvents = maxMathEvents;
        this.buffer = buffer;
    }
    ...
    @Override
    public void run() {
        for(int i=0; i< maxMathEvents; i++){
            Random random=new Random();
            int priority= random.nextInt(100);
            MathEvent event = new MathEvent("MathProducer-" + Thread.currentThread().getName(), priority);
            buffer.put(event);
        }
    }

}
 MathProducer responsibility is to generate some MathEvents and give them random priority. 

   The same action we do with consumer. The individual MathConsumer  consumes all new MathEvent that are available to him in the queue (queue is responsible for MathEvents prioritising). 
public class MathConsumer implements Runnable {
    ...
    public MathConsumer(MathPriorityTransferQueue< MathEvent > buffer) {
        this.buffer = buffer;
    }
    ...
    @Override
    public void run() {
        while(buffer.peek() != null){
            try{
                MathEvent event = buffer.take();
                logger.debug("MathConsumer-" + Thread.currentThread().getName() +
                        " : " + event.getThread() + " priority: " + event.getPriority());
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}
  Now all parts participating on the example are defined and we can move to the implementation of the example heart MathPriorityTransferQueue . Here is worthy to point out atomic counter implementation to avoid possible race-condition and that the counter shouldn't be implemented by using volatile. Volatile quarantines only happens-before (write-read) but not atomicity at all. 
public class MathPriorityTransferQueue<E> extends PriorityBlockingQueue<E> implements TransferQueue<E> {
    ...
    private AtomicInteger counter;
    private LinkedBlockingQueue<E> transferList;
    private ReentrantLock lock;
    ...
    public MathPriorityTransferQueue(){
        counter = new AtomicInteger(0);
        lock = new ReentrantLock();
        transferList = new LinkedBlockingQueue<>();
    }
...
  The counter is used to control important operations that are provided by our DataStructure
1. take() operation to get an MathEvent from the data structure (increment counter) and when operation is finished than decrement the counter.
...
@Override
public E take() throws InterruptedException {
   lock.lock();
   counter.incrementAndGet();
   E result = transferList.poll();

   if(result == null){
     lock.unlock();
     result = super.take();
     lock.lock();
   } else {
      synchronized (result){
        result.notifyAll();
      }
   }
   counter.decrementAndGet();
   lock.unlock();
   return result;
}
...
2. peek() operation to retrieve but don't remove head of the queue if the queue is empty then returns null. Here we need to take care also about the transferList that represents Blocking queue to store accidentally transfered MathEvents
@Override
public E peek() {
   lock.lock();
   E eventMain = super.peek();
   E eventTrans = transferList.peek();
   E result = eventMain != null ? eventMain : eventTrans;
   lock.unlock();
   return result;
}
3. tryTransfer(E e) which tries to transfer MathEvent to the consumer if any is waiting for.
...
@Override
public void transfer(E event) throws InterruptedException{
   lock.lock();
   if(counter.get() != 0){
      put(event);
      lock.unlock();
   } else {
      transferList.add(event);
      lock.unlock();
      synchronized (event){
         event.wait();
      }
   }
}
...
4. tryTransfer(E event, long timeout, TimeUnit unit) throws InterruptedException method tries to provide MathEvent to the Consumer who is waiting maximum time. If not successful then transferList for someTimeout or thread is interrupted. 
...
@Override
public boolean tryTransfer(E event, long timeout, TimeUnit unit) throws InterruptedException {
   lock.lock();
   if(counter.get() != 0){
      put(event);
      lock.unlock();
      return true;
   } else {
       transferList.add(event);
       long someTimeout = TimeUnit.MILLISECONDS.convert(timeout, unit);
       lock.unlock();
       event.wait(someTimeout);
       lock.lock();
       if(transferList.contains(event)){
           transferList.remove(event);
           lock.unlock();
           return false;
       }else{
           lock.unlock();
           return true;
       }
    }
}
...
5. transfer(E e) throws InterruptedException method transfer successfully when Consumer is waiting for MathEvent if not it it put MathEvent into the transferList and waits until MathEvent is taken out. 
...
@Override
public void transfer(E event) throws InterruptedException{
   lock.lock();
   if(counter.get() != 0){
       put(event);
       lock.unlock();
   } else {
       transferList.add(event);
       lock.unlock();
       synchronized (event){
          event.wait();
       }
   }
}
...
  
and some more as you can find in the example source code but they are not critically important to make this example work (hasWaitingConsumer, getWaitingConsumerCount). 
   As DataStructure is implemented we need to implement very important MathEvent method compareTo(MathEvent event) as MathEvent implements Comparable<MathEvent> interface.  
 In the example definition data structure orders elements based on their priority.  
public class MathEvent implements Comparable<MathEvent>{
    ...
    @Override
    public int compareTo(MathEvent event) {
        if (this.priority > event.getPriority()) {
            return -1;
        } else if (this.priority < event.getPriority()) {
            return 1;
        } else {
            return 0;
        }
    }
    ...
}
   Having all those steps done we can run successfully MathPrioProdConMain class. and we get following result (of course based on our setup of CONSUMERS and PRODUCERS with MathEvents )
...
MathConsumer-Thread-6 : MathProducer-Thread-4 priority: 71
MathConsumer-Thread-6 : MathProducer-Thread-2 priority: 71
MathPrioProdConMain: Buffer waiting consumers = 0
MathConsumer-Thread-6 : MathProducer-Thread-0 priority: 71
MathConsumer-Thread-6 : Transfer Math Calculation Event  priority: 1
MathConsumer-Thread-6 : MathProducer-Thread-0 priority: 70
MathConsumer-Thread-6 : MathProducer-Thread-0 priority: 70
...

   More comments can be found inside the source code that is available over my github account (example source code)
Enjoy Multi-threading !

Thursday, September 4, 2014

Threading: Producer Consumer without Synchronisation with SharedBuffer

Java becomes (especially java8 :) to be always more challenging when it goes more into the multi-threading questions and tasks. 
  The Producer-Consumer problem is one of the classical ones to solve. The Producer and Consumer share buffer where the Producer put some entity into and consumer uses such product and process it. 
  Following example shows  capacity limited SharedBuffer with the Conditions (interface java.util.concurrent.locks.Condition since java5) under which can be something inserted and read (has buffer free space, has buffer something to read). 
public class SharedBuffer {
  ...
  private ReentrantLock lock;

  private Condition lines;
  private Condition space;
  ...
  public SharedBuffer(int maxSize){
...
  We do not use synchronised LinkedList for data to be exchanged between Producer and Customer. Also we use only one Producer to store some product into the SharedBuffer (in example: Line of Characters) and more Customers to process such products (represented by CUSTOMER_NUMBER in the code)
  The ProdConOneMain class is responsible for the data preparation for Producer who is responsible for adding product into the SharedBuffer under certain condition (is there free space). 
...
public static void main(String... args){  
   logger.debug("Start ProdCon One Example");

   CharacterLines characterLines = new CharacterLines(MAX_LINES, MAX_LINE_CHARACTERS);
   SharedBuffer buffer = new SharedBuffer(BUFFER_SIZE);

   Producer producer = new Producer(characterLines, buffer);
   Thread producerThread = new Thread(producer, "Producer ");

   Thread[ ] consumerThreads = new Thread [ CONSUMER_NUMBER ];
   producerThread.start();
   for(int i=0; i < CONSUMER_NUMBER; i++){
     consumerThreads[i].start();
   }
...
  When condition for insertLine is not satisfied it calls await() method on Condition space to let producer wait until there space is there. Producer thread will be woken up when Consumer treads call signalAll() on this condition. When Producer is able to store new line he calls signalAll() on the lines condition, because it may happen that Consumers are waiting for new lines. SharedBuffer implementation uses ReentrantLock which is the implementation of the Lock interface and provides re-entrant mutual exclusion Lock with the same basic behaviour and semantic as the implicit monitor lock access used by synchronised methods. ReentrantLock provides more controls (ability to lock interruptibly, timeout during waiting for lock, flexibility... ), as Thread doesn't need to block infinitely, and can specify fairness property (not in this example).
...
public void insertLine(String line){
  lock.lock();

  try{
    while(buffer.size() == maxSize){
      space.await();
    }
    buffer.offer(line);
    logger.debug(Thread.currentThread()
                    .getName() + ": BUFFER Inserted Line: " + buffer.size());
    lines.signalAll();

  } catch (InterruptedException e){
     logger.error(e.toString());
  } finally {
     lock.unlock();
  }
}
...
   As Consumer is accessing the SharedBuffer he needs to communicates over it with Producer. Consumer task is to take the product (line of characters) form the SharedBuffer and process it. It does make sense to access when there is something to read and also by Consumer behaviour we make the space for producer and notify him by calling SignalAll() method on the space Condition.  
public  String getLine(){
  String result = null;
  lock.lock();

  try{
    while((buffer.size() == 0) && (hasPendingLines())){
      lines.await();
    }

    if(hasPendingLines()){
       result = buffer.poll();
       logger.debug(Thread.currentThread().getName() + ": BUFFER Line to Read: " +  buffer.size());
       space.signalAll();
    }

  }catch (InterruptedException e){
    logger.error(e.toString());
  } finally {
    lock.unlock();
  }

  return result;
}
   By having defined SharedBuffer with methods relevant for consumer and producer we can create both of them by implementing Runnable interface: 
public class Producer implements Runnable{

    private CharacterLines characterLines;

    private SharedBuffer  buffer;

    public Producer(CharacterLines characterLines, SharedBuffer buffer) {
        this.characterLines = characterLines;
        this.buffer = buffer;
    }

    @Override
    public void run() {
        buffer.setPendingLines(true);
        while(characterLines.hasMoreLines()){
            buffer.insertLine(characterLines.getNextLine());
        }
        buffer.setPendingLines(false);
    }
}
...
...
...
public class Consumer implements Runnable{

    private SharedBuffer buffer;
    private Random random;

    public Consumer(SharedBuffer buffer) {
        this.buffer = buffer;
        this.random = new Random();
    }
    ...
    private void processLine(String line){
        try{
          ...  
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
Currently we have ready actors in our example and we can run it (viz. ProdConOneMain). The example output should look like following. 
Start ProdCon One Example
Waiting Lines: 10
Producer : BUFFER Inserted Line: 1
Waiting Lines: 9
Producer : BUFFER Inserted Line: 2
Waiting Lines: 8
Consumer 0: BUFFER Line to Read: 1
Consumer 1: BUFFER Line to Read: 0
Producer : BUFFER Inserted Line: 1
...

You can check the code over my githug. 
This example should be the beginning of the bigger series. 
Enjoy Multi-Threading

Tuesday, August 19, 2014

Spring 4 Scala MVC without Web.XML - Tiles3 : WebConfig class configuration with Localisation


Img.1.:: Spring 4 Scala project structure with Tiles 3 and l18n(l10n) 
  Enabling Apache Tiles3 needs little more changes in the project (Img.1.) configuration. The WebConfig scala class needs to extended by WebMvcConfigurerAdapter (already touched in the previous post: Scala Spring 4 MVC without Web.XML configuration with static resources handling). 
  We also define the set of annotations we do need to be scanned over the project package. The reason of such definition is to show possibilities of Scala configuration of the Spring Framework itself.
@Configuration
@ComponentScan(basePackages = Array("miko.scala.helloexample"), useDefaultFilters = false, includeFilters = Array(
  new ComponentScan.Filter(`type` = FilterType.ANNOTATION,
    value = Array(classOf[Controller], classOf[Service]))))
@EnableWebMvc
class WebConfig extends WebMvcConfigurerAdapter{
...
  In WebConfig we do replace @Bean setupViewResolver(), which implements the ViewResolver interface that allows directly resolved symbolic view names,  by getTilesConfigurer @Bean, which provide TilesConfigurer. 
  The TilesConfigurer class provide the tempting mechanism for web project (application) using JSP and other front-end engines. 
@Bean
def getTilesConfigurer: TilesConfigurer = {
  val configurer:TilesConfigurer = new TilesConfigurer()
  configurer.setCheckRefresh(true)
  configurer.setDefinitions({"/WEB-INF/pages/tiles.xml"})
  configurer
}
 And additionally we have  to define ViewResolver for Tiles by getTilesViewResolver @Bean definition
@Bean
def getTilesViewResolver: ViewResolver = {
  val resolver:TilesViewResolver = new TilesViewResolver()
  resolver.setContentType("text/html")
  resolver
}
The full configuration needs to define and override also couple of other Beans connected with our localisation (l10n/l18n) intent. The final WebConfig Scala class will look in following manner. 
@Configuration
@ComponentScan(basePackages = Array("miko.scala.helloexample"), useDefaultFilters = false, includeFilters = Array(
  new ComponentScan.Filter(`type` = FilterType.ANNOTATION,
    value = Array(classOf[Controller], classOf[Service]))))
@EnableWebMvc
class WebConfig extends WebMvcConfigurerAdapter{

  private val logger = LoggerFactory.getLogger(getClass)

  override def configureDefaultServletHandling(configurer: DefaultServletHandlerConfigurer) = {
    configurer.enable()
  }

  override def addViewControllers(registry: ViewControllerRegistry): Unit ={
    registry.addViewController("/")
  }

  @Bean
  def getTilesViewResolver: ViewResolver = {
    val resolver:TilesViewResolver = new TilesViewResolver()
    resolver.setContentType("text/html")
    resolver
  }

  @Bean
  def getTilesConfigurer: TilesConfigurer = {
    val configurer:TilesConfigurer = new TilesConfigurer()
    configurer.setCheckRefresh(true)
    configurer.setDefinitions({"/WEB-INF/pages/tiles.xml"})
    configurer
  }

  override def addInterceptors(registry:InterceptorRegistry) = {
    val localeChangeInterceptor = new LocaleChangeInterceptor()
    localeChangeInterceptor.setParamName("lang")
    registry.addInterceptor(localeChangeInterceptor)
  }

  /**
   * equivalents for  tags
   * @param registry
   */
  override def addResourceHandlers(registry: ResourceHandlerRegistry) = {
    logger.debug("Resources HANDLER")
    registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(31550522)
    registry.addResourceHandler("/WEB-INF/layouts/**").addResourceLocations("/WEB-INF/layouts/").setCachePeriod(31550522);
    registry.addResourceHandler("/WEB-INF/pages/template/**").addResourceLocations("/WEB-INF/pages/template/").setCachePeriod(31550522);
  }

  @Bean(name = Array("localeResolver"))
  def getLocaleResolver: LocaleResolver = {
    val cookieLocaleResolver: CookieLocaleResolver = new CookieLocaleResolver()
    cookieLocaleResolver.setDefaultLocale(new Locale("en"))
    cookieLocaleResolver
  }

  override def configureContentNegotiation(configurer: ContentNegotiationConfigurer) = {
    configurer.favorPathExtension(true).ignoreAcceptHeader(true)
        .useJaf(false).defaultContentType(MediaType.TEXT_HTML)
        .mediaType("html", MediaType.TEXT_HTML)
        .mediaType("xml", MediaType.APPLICATION_XML)
        .mediaType("json", MediaType.APPLICATION_JSON)
  }

  @Bean(name = Array("messageSource"))
  def getMessageSource: MessageSource = {
    val msgSrc:ReloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource()
    msgSrc.setBasename("classpath:/messages/message")
    msgSrc
  }
}
  After having enabled Tiles with l10n/l18n on the back-end side we can move to front-end and tell Tiles which kind of template it should use to display pages correctly to the user (Img.1. ~ project structure). Of course, with CSS or possible JavaScript libraries. 
But I'll do this in next blog post. 
  Now Enjoy configuring your WebConfig scala class. 

project: miko-spring-scala on github

Friday, August 8, 2014

HowTo:: Scala Spring 4 MVC without Web.XML configuration with static resources handling

This blog post is fully focused on Scala language and how to use it for Spring 4 based application development. I'll  modify application I've used over previous blog post series ("Spring4Netty without Web.XML configuration on Java 8.."). 

  Although Spring Framework is so popular in the Java land, there is no reason why not to use Scala language for the EE application development. 
In case when the Applicaiton front-end uses popular AngularJS Framework I personally find Scala usage much "easier" (ps: still lot of work :) and comfortable to understand back-end and front-end functionality as javascript uses callbacks. 

for building new Spring4 Scala application we use following tools: 
of course Jetty server can be change to the Tomcat one. 
  Whole sample application has been developed in IntelliJ IDEA 13.x IDE. 
After we have created the SBT project under IntelliJ we need to import some plugins.
Img.1.:: SBT Project Structure - plugins.sbt
  To import them we change sbt plugins setup by modifying appropriate file (Img.1.) in the following manner. 
addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "1.0.0-M4")

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
  We have now added two plugins  xsbt-web-plugin and sbt-idea.  The xsbt-web-plugin is used for building web enterprise application. Both of them are extensions to SBT building tool.

  Next step is to add necessary libraries dependences into the build.sbt file. As we want to link also static resources, for the front-end development, the number of needed libraries will be bigger. 
  The build.sbt file will look after our modification like following example.
name := "miko-spring-scala"

version := "1.0"

scalaVersion := "2.10.3"

libraryDependencies ++= Seq(
  "org.springframework" % "spring-webmvc" % "4.0.6.RELEASE",
  "org.springframework" % "spring-context" % "4.0.6.RELEASE",
  "org.springframework" % "spring-context-support" % "4.0.6.RELEASE",
  "javax.servlet" % "javax.servlet-api" % "3.0.1" % "provided",
  "javax.servlet" % "jstl" % "1.2" % "compile",
  "org.eclipse.jetty" % "jetty-webapp" % "9.1.0.v20131115" % "container, compile",
  "org.eclipse.jetty" % "jetty-jsp" % "9.1.0.v20131115" % "container"
)

jetty(port = 9090)
here is visible which kind of servlet engine provider will be used (in case of Tomcat change jetty() to tomcat()). 

  When the SBT application is ready we will use commands to start/stop it.
... SBT console ...
container:start

container:stop
...
  All libraries and plugins are ready and we can start building Spring4 Scala Application without using Web.XML and with linked static resources for the next front-end development. Web.xml file will look almost empty in contracts to XML configuration.
...
Miko Scala Spring 4 - Example without Web.xml 
...
  The application package structure is very similar to the previous ones. I want to keep consistency and get possibility to have the reference to the Java implementation (Img.2.) in case of possible comparison.
Img.2.:: Example Spring4 Scala application packages structure

Now I simply turn the Java into the Scala in several steps

1. Scala WebConfig will look like following

..
@Configuration
@ComponentScan(basePackages = Array("miko.scala.helloexample"))
@EnableWebMvc
class WebConfig extends WebMvcConfigurerAdapter{

  override def addResourceHandlers(registry: ResourceHandlerRegistry) = {
    registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(31550522)
  }

  override def configureDefaultServletHandling(configurer: DefaultServletHandlerConfigurer) = {
    configurer.enable()
  }

  @Bean
  def viewResolver = {
    val viewResolver = new InternalResourceViewResolver
    viewResolver.setViewClass(classOf[JstlView])
    viewResolver.setPrefix("/WEB-INF/pages/")
    viewResolver.setSuffix(".jsp")
    viewResolver
  }
}
2. Scala WebInitializer which help us setup ServletContext at the starup 
class WebInitializer extends WebApplicationInitializer{

  override def onStartup(servletContext: ServletContext) = {
    val ctx = new AnnotationConfigWebApplicationContext
    ctx.register(classOf[WebConfig])

    ctx.setConfigLocation("META-INF/spring/app-spring-scala.xml")

    val servlet: Dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx))
    servlet.addMapping("/")
    servlet.setLoadOnStartup(1)
  }
}
3. Scala HelloExampleController class annotated by @Contoller annotation. The Annotation takes care about HttpServletRequest and Responses. This example controller also shows how Services are @Autowired when Scala language is used. 
@Controller
@RequestMapping(Array("/"))
class HelloExampleController @Autowired() (exampleService: ExampleService, exampleImportService: ExampleImportService){

  @RequestMapping(method =  Array(RequestMethod.GET))
  def hello( model: Model) = {
    model.addAttribute("message", "Here is Scala with ServiceName = " + exampleService.serviceName)
    model.addAttribute("importMessage", "Here is Scala with ServiceImportName = " + exampleImportService.name)
    "hello"
  }
}
4. Scala Example...Services used by HelloExampleController are done over quite powerful Traits implementation. Traits are very similar to Java Interfaces but they allows partial implementation which makes them much more powerful (out of blogpost scope ;).  
trait ExampleImportService {
  def name: String
}
-----
class ExampleImportServiceImpl extends ExampleImportService{
  override def name: String = "Imported service over XML"
}

after all those steps we've ready Scala Spring 4 application to test. We've also source code to compare with previous Java8 implementation. Scala does the great work because thinking in functions and callbacks intentions makes whole development more strait forward. 
Enjoy sample application ! (GitHub miko-spring-scala)

Monday, July 21, 2014

Lego Mindstorm EV3 -> Java8, LeJOS and Gradle with taste of Maven

Img.1. - Let's turn on the LegoBrick again and make remote project based on Gradle
The previous post about Lego MindStorm EV3 has been focused on its installation. I've touched how to install JavaJDK setup Wifi on LegoBrick + small sample "hello world" project.
This blog post is about setting up little more advanced project.

The simple HelloWorldGradle Remote project uses:
The legoBrick, connected to the WiFi, is possible to access remotely without problem using Java Remote Method Invocation, shortly RMI.
RemoteEV3 ev3Brick = new RemoteEV3(EV3BRICK_IP);
ev3Brick.setDefault();
Before we start working on the sample source code we create Gradle project. We can randomly choose the project as HelloWorldGradle. Important is to setup Gradle property file "build.gradle" correctly
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
version = '1.0'
repositories {
    mavenCentral()
    mavenLocal()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'lejos:ev3classes:1.0-SNAPSHOT'
    compile ('log4j:log4j:1.2.17'){
        exclude group: "com.sun.jdmk", module: "jmxtools"
        exclude group: "javax.jms", module: "jms"
        exclude group: "com.sun.jmx", module: "jmxri"
    }
    compile 'org.slf4j:slf4j-log4j12:1.7.5'
}
jar library "ev3classes" is very important!
This library creates the gateway into the lego hardware by using RMI because EV3 acts as the RMI Registry. You need to create it by yourself.
lejos
  ev3classes
  jar
  1.0-SNAPSHOT
  
    
      lejos
      dbusjava
      1.0-SNAPSHOT
    
    
      net.java.dev.jna
      jna
      3.2.7
    
  
  
    ${basedir}/src
    ev3classes
    install
    
      
        maven-compiler-plugin
        ${maven.compiler.version}
        
          ${java.version}
          ${java.version}
        
      
      
        org.apache.maven.plugins
        maven-jar-plugin
        ${maven.jar.version}
      
      
        maven-assembly-plugin
        ${maven.assembly.version}
        
          ${project.build.finalName}-${project.version}.jar
          
            jar-with-dependencies
          
        
        
          
            make-assembly
            package
            
              single
            
          
        
      
    
  
This library can be created by maven with usage of maven-assembly-plugin. The final *.jar file can then easily imported into the local maven cache.
Now we are ready to remotely access our Lego MindStorm robot and in our case let him to play some short song.
RemoteEV3 ev3Brick = new RemoteEV3(EV3BRICK_IP);
            ev3Brick.setDefault();
            Audio soundProcessor = ev3Brick.getAudio();
            List tones = new ArrayList<>(asList(0,1,2,3,4,5));
            tones.forEach(tone -> {
                soundProcessor.systemSound(tone);
            });
The sample LejOS remote project HelloWorldGradle is available here (GITHub).
Enjoy !

Wednesday, July 2, 2014

Lego Mindstorm EV3 -> LeJOS installation

Img.1.: let's begin and rock (part of my toys)
I do believe that the default lego software for EV3 is pretty awesome ;) but my interest belongs to java virtual machine which implies installation of LeJOS. 

required:
1. download leJOS_EV3_0.8.1-beta.tar.gz (last available)
2. download Java for Lego Mindstorm EV3 = ejdk-8-fcs-b132-linux-arm-sflt-03_mar_2014.tar.gz
3. 6 x AA Batteries 1.5V - rechargeable -> almost green energy

Let's prepare LeJOS SD card for the mission:
1. from the manual:  card must be bigger than 2GB and FAT32 
2. unpack leJOS file -> go to the folder -> copy the lejosimage.zip file to the SD Card
3. unpack lejosimage.zip
4. copy downloaded Java SE file to the CD-Card (SD Card root)
5. put SD-CARD to the Lego Brick and finally startup

To work with LeJOS comfortably is required to have WiFi connection via dongle (Img.2.). 
Then is possible to deploy directly for the IDE (IntelliJ IDEA 13.x in my case) to Lego Brick. 

Img.2.:: supported NetGear N150 USB Wireless Adapter
Prepare Wifi Connection:
1. Boot Brick and select Control -> Wifi -> select network with WPA2 
Img.3.:set the wifi password
2. Create a sample maven program (how to will be the topic of the next blog post)
Img.4.: favorite IDE IntelliJ IDEA 13.x with Maven Lego Project inside and upload command
for those who want to use maven directly for the deployment they need to configure maven-antrun-plugin

So now we have Lego Mindstorm EV3 ready for read development and sensors usage... 

  1. HelloWolrdGradle Remote Hardware usage example project
  2. more is coming soon...


Friday, June 27, 2014

Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.5:: Application Servers and logging

  
img.1.: official log



  This part about selected application start normally, just by downloading both of them and setting them up in your favorite IDE (img.2.) which is in my case IntelliJ IDEA (img.1.)


img.2.: Application Serves bond with IDE
   So here we go, app servers inside (like the intel processor). What it good to point out already at the beginning is the servlet-api versions support. 

Don't try to run this example project (GiTHub repository) on Tomcat 6.x. Why ? It's because Tomcat 6.x supports servlet-api ver. 2.5, and as you can find in the parent pom.xml file, the version 3.x of servlet-api is required. It's because of features we have touched earlier  (part.4.) .

   Almost every project uses some logging framework, in project case we use Simple Logging  Facade (SLF4j) together with well known Log4j. I don't want to write a lot about application logging design it's the another story (this project is just hot setup of usage). 
   What I want to touch here is the topic "How to enable logging under JBoss WildFly per deployment for SLF4j" 
   It 's because by the default installation of WildFly it not allowed. You see actually any Logger output in your SystemOut or log file.  

Let's edit in following way the ../standalone.xml file that you can find in the WildFly directory
   ...
   
        
            
            <!-- IMPORTANT LINE --> 
            

            
                
                
                ...
                
                
            
            
        ...
        
   ...
   
   ...
The logging is not fully setup but important details are there so you can enjoy my project. 
s4netty project GiTHub repository



ps: Also I've not merged JUnit, Mockito  tests in case I didn't want to touch this topic in this blogpost. 

Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.4:: Base - Spring MVC without Web.xml usage 2

  
img.1.: s4netty-web deployment result

  The base web module s4netty-web is almost similar to  the previous worker module (s4netty-worker) from the perspective of the Spring MVC configuration. 

  The main difference is the Servlet Context setup. In the usage 1 (s4netty-worker) everything was defined by appropriate annotations. In the usage 2 (s4netty-web) the Servlet Context is defined in traditional XML way from perspective of servlet definition. 
   ...
   

   
     
     
   
   ...
  The WebApplicationContext is instantiated by XmlWebApplicationContext which (img.2.) extends AbstractRefreshableWebApplicationContext. Here we go deeper to all WebApplicationContext implementations which are supposed to instantiate themselves  according to the provided configuration over the interface. 
img.2.: s4netty-web module structure 
  The purpose of  XmlWebApplicationContext  class is to load WebApplicationContext from the XML configuration files.  
public class WebInitializer implements WebApplicationInitializer {
    ...
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ...
        XmlWebApplicationContext ctx = new XmlWebApplicationContext();
        ctx.setConfigLocation("WEB-INF/spring/dispatcher-servlet.xml");
        ctx.setServletContext(servletContext);
        ...

  What was also earlier only briefly touched (part1:: img.0.) is the ability of s4netty-web module to communicate with s4netty-worker  module utilising JSON serialisation/deserialisation over the service and pre-defined port P.

  The s4netty-web module uses instantiation of the RestTemplate which simplifies communication with HTTP server, its synchronous and it enforces RESTful principles, what is obvious from it name RestTemplate ;), but honestly it's pretty handy to use. 
@Controller
@RequestMapping("/")
public class MainController {
    ...
    RestTemplate restTemplate = new RestTemplate();

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
    ... 
  Let's ask s4netty-worker module for the car list using newly instantiated RestTemplate
    ...
   private List getSimpleCarList(){
        List result = new ArrayList<>();
        try{
            result = restTemplate.getForObject(S4_NETTY_WORKER_LINK, List.class);
            logger.debug("getSimpleCarList result= " + result);
        }catch (HttpClientErrorException e){
            logger.error("getSimpleCarList jsonCarList e= " + e);
        }
        return result;
   }
   ...

  Now we have ready two deployable war files and we can take a look at the result. But before we do so it's fair to mention why such configuration works.  The reason is that we do use for
 registering servlets in both cases  
   ...
   import javax.servlet.ServletRegistration.Dynamic
   ...

interface which uses one of addServlet()  methods into ServletContext. The interface is the part of javax.servlet-api imported previously by Maven into the project. 
  ...
  
        javax.servlet
        javax.servlet-api
        3.0.1
        provided
  
  ...
After this final hit we do know all and we can start with deployment on application servers (JBoss WildFly & Tomcat 8 were selected).
Let's make it in the Next part (Click Next) 

s4netty project GiTHub repository

  

Thursday, June 26, 2014

Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.3:: Worker - Spring MVC without Web.xml usage 1

  

  Now we are entering the last stage in case of s4netty-worker.  Although we have already enabled Netty usage under spring ecosystem, we still want to provide some simple web (Spring MVC) for this module to see the output from the Netty JSON service.  


s4netty-worker - spring mvc output
img.1.: Spring MVC output from s4netty-worker

  I won't go into the details how to build up the basic Spring MVC project because it's easy to find out by our friend google. What is interesting is how to not use web.xml (Deployment Descriptor Element which determinates the urls mapping to appropriate servlets ) definition alias get some freedom for Java web applications. In case of s4netty-worker we do almost everything over annotations, only beans will be defined in separate app-worker-config.xml file (viz. img.2).  


img.2.: s4netty-worker module structure 

  Before we initiate web application itself by WorkerWebInitializer, that implements WebApplicationInitializer, we need to do some configuration here. (ps. WebApplicationInitializer allows us to configure servlets, filters and etc. by @Override onStatrup method)
public class WorkerWebInitializer implements WebApplicationInitializer {
   ...
   @Override
   public void onStartup(ServletContext servletContext) throws ServletException {
       AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
       ctx.register(WorkerWebConfig.class);
       ctx.setConfigLocation("spring/app-worker-config.xml");
   ...
without such configuration our WorkerWebInitializer is useless because after WebApplicationContext instantiation Spring looks for Spring configuration files annotated by @Configuration annotation. For this purpose we create WorkerWebConfig class. 


@Configuration
@ComponentScan("com.miko.s4netty")
@EnableWebMvc
public class WorkerWebConfig {
   ...
   @Bean
   public UrlBasedViewResolver setupViewResolver() {
   ...

in WorkerWebConfig class we tell to the Spring where it should look for the rest of components (example: classes annotated by @Controller). By using @EnableWebMvc we import  the DelegatingWebMvcConfiguration which delegates us to all beans reliable for WebMvcConfigurer and our customisation.  At the end the method setupViewResolver allows us to briefly solve symbolic link name to the URL. Pretty cool, isn't it ?

  The one of the impact is that we can store our favorite ;) *.jsp pages in the appropriate directory (img.2.)

  Now we have ready s4netty-worker web output with appropriate Beans initialisation, which is done by ServletContext configuration definition located in the file spring/app-worker-config.xml

  

  
    
  
   And finally our result looks according to the img.1. mentioned at the beginning of this part. 
The big moment is here because we are moving to the stage 2 (s4netty-web).
The next part (Click Next) 

s4netty project GiTHub repository 
  

Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.2::netty and JSON

  

  In the introduction (part.1). we've pictured out the basic idea of the project  abstraction(car exchange;). In this part we are going to configure Netty as the part of maven spring module (s4netty-worker). 

  Let's draw the agenda that allows us to start netty server under the spring ecosystem:
  1. initiate serverBootstrap object and configure the Netty server from properties file (worker-server.properties).
  2. configure the group, channel, handler and some channel options (ps: keep alive signal has been ignored in this project)
  3. Channel initialisation  
  4. Netty JSON service response
  5. Netty JSON request handling
  Let's begin and configure WorkerNettyServer which contains the instance of the serverBootstrap bean (add.1.). ServerBootstrap definition can be found in WorkerNettyConfig class.
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
All necessary  properties for WorkerNettyConfig class used by serverBoostrap (add.1.) bean initialisation are taken from the config the file (worker-server.properties) 
@Configuration
@ComponentScan("com.miko.s4netty")
@PropertySource("classpath:worker-server.properties")
public class WorkerNettyConfig {
  To make possible reading from the *.properties file   
@Value("${tcp.port}")
private int tcpPort;
we need to have access to the current Spring Context. This goal we achieve by using property sources place holder :
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
Now WorkerNettyConfig knows all values for its initiation 
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
   ServerBootstrap b = new ServerBootstrap();
After all those configuration steps, we get access, inside the WorkerNettyServer class, to the bean "serverBootstrap" by using Qualifier annotation for candidate Bean serverBootstrap (add.2.). 
@Component
public class WorkerNettyServer {
    ...
    @Autowired
    @Qualifier("serverBootstrap")
    private ServerBootstrap serverBootstrap;
    ...
and we can have quickly assign all serverBootstrap properties (add.2.) 
@Bean(name = "serverBootstrap")
public ServerBootstrap bootstrap() {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup(), workerGroup())
    .channel(NioServerSocketChannel.class)
    .childHandler(workerInitializer);
  So move forward and initiate the Channel and it's pipeline in the WorkerInitializer class (add.3.). Of course all according to our "master plan" to have JSON service listening on the specified port P and exchanging car list with base.
  In this case we need totell Netty how it should deal with such type of the request by utilising HttpServerCodec (add.4.). 
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(workerProviderHandler);
  Currently it looks like JSON service  is listening on the port P and everything is almost ready to go. Well not exactly, there is still a some work do. 
  One good question could be about some missing definition :) oh, yes, where is 
workerProviderHandler ? (add.5.)  and here we go.
@Component
@ChannelHandler.Sharable
@Qualifier("workerProviderHandler")
public class WorkerProviderHandler extends ChannelInboundHandlerAdapter {
  ...
  @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest req = (HttpRequest) msg; 
  ...
  The workerProviderHandler is the extension of ChannelInboundHandlerAdapter and it handles our request and provides the JSON response to the requestor (s4netty-web). 
The request itself consists from results provided by spring service. 
...
@Autowired
SimpleCarService simpleCarService;
...
Gson library has been used to serialise and deserialise POJO objects.

After defining handler and assigning to the pipeline we are done with Spring-Netty Configuration and ready to define module (s4netty-worker) Spring MVC without Web.xml usage. 
Sure, in the next part (Click Next) 

s4netty project GiTHub repository