Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, June 23, 2016

WildFly Swarm JSF example

WildFly Swarm now has a generator. Thus generating a pom.xml for JSF application bundled into one jar file has become super easy. See example below

Wednesday, October 15, 2014

Wednesday, September 3, 2014

Glue together git, feature branches, Jenkins and Redmine

Feature branches or topic branches are useful thing to keep master stable and to introduce atomicity and isolation to what you are doing. But when you commit your work and set task to Resolved state it doesn't mean that this fix is ready to be tested by QA team. There is a time lag between the moment you resolve an issue in issue tracker and it is rolled out on test environment: your lead needs to review your changes, QA lead needs to make a build that will grab your change and put it to the testing env.
There is a simple solution to this issue though. First of all a custom field has been added to the tracker (issue/bug/feature/ticket) in Redmine namely Issue fixed build number (ok, it must have been called Issue fixed commit), when someone resolves a ticket it fills in the last commit in corresponding feature branch. If one sees this commit in some branch he can say with confidence that this branch contains changes for this feature (after you merge feature branch to the master you'll see there the same commit hash, it doesn't hold for rebase).
Secondly you'll need the proprietary JenkinsRedmine plugin, see Building a plugin. The plugin actually does two simple steps:
  1. Gets all commits in fetched by Jenkins brunch issuing git log command
  2. Gets all issues in Resolved state for given project from Redmine (see Redmine REST API for Issues), checks which Issue fixed build numbers are in git log
Then it just draws a table like below. It omits issues that are not found in git log. Thus QAs get the list of issues to check on test environment.

Friday, January 31, 2014

OSGi based web applications

OSGi attracts a lot nowadays but being a well spread technology for lightweight embedded systems and such things as Eclipse, Glassfish, etc. it is not so well spread in enterprise scenarios (despite existence of OSGi Enterprise Specification, but I've still heard success story about Apache Karaf production usage). OSGi as it breaks your application into modules and provides service registry and event bus is a right way to maintain medium to huge codebase and overcome issues with integration in big distributed teams. Live code and configuration updates support is what must have if you want availability. See beautiful article Why OSGi service dynamics are useful.
The question I was wondering recently is there a good web application framework that plays nice with OSGi (something like JSF in Java EE or Spring MVC). I found the answer on stackoverflow:
I'm developing OSGi based web applications on a daily base. We have a slightly alternative approach which works very well:
  1. RESTful web services implemented using OSGi services 
  2. Modular AngularJS based user interface, packaged in OSGi bundles Although AngularJS is obviously not a Java web framework, it fits modern web development perfectly. If you are familiar with Java MVC web frameworks it's also easy to understand Angular.
The main question is how to modularize your code. At the backend we implement everything as OSGi services. RESTful webservices are implemented with JAX-RS using the Amdatu (amdatu.org). With Amdatu you can use JAX-RS in a way that fits OSGi well. Each RESTful resource and lower level services are packaged in separate bundles as well, so the backend is fully modular.
To modularize the UI itself we separate functional different parts of the application as different Angular applications. Each Angular application is packaged in separate bundles as well, using the Amdatu resource provider.
The idea is a good one regardless OSGi part. If you are about to build a web app consider providing just REST endpoint and put on top of it something like AngularJS/Backbone/Knockout. And providing REST endpoint itself is a simple task with amdatu-web.

Sunday, January 12, 2014

Tuesday, January 7, 2014

Inversion of Control for 6 year old

If you can't explain it to a six year old, you don't understand it yourself. (Albert Einstein)
Despite release 1.0 of Spring was in 2003 the new wave of programmers seem to misuse the concept. Below we'll consider a small example to explain.

Monday, December 30, 2013

Performance analysis of our own full blown HTTP server with Netty 4

In previous post Let's do our own full blown HTTP server with Netty 4 you and I were excited by creation of our own web server. So far so good. But how good?

Let's do our own full blown HTTP server with Netty 4

Sometimes servlets just doesn't fit you, sometimes you need to support some protocols except HTTP, sometimes you need something really fast. Allow me to show you the Netty that can suit for these needs.
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. (http://netty.io/)
Netty has everything one needs for HTTP, thus web server on Netty is like a low hanging fruit.
First of all you need to understand what pipeline is, see Interface ChannelPipeline. Pipeline is like processing line where various ChannelHandlers convert input bytes into output. Pipeline corresponds one to one to connection, thus ChannelHandlers in our case will convert HTTP Request into HTTP Response, handlers will be responsible for such auxiliary things like parsing incoming packets and assembling outcoming and also call business logic to handle requests and produce responses.
Full source is available.

Wednesday, December 25, 2013

Good Transfer Object Hierarhy

One approach worked well for me. Allow me to show by example.
class UserTO {
 private Long id;
 ...

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }
 ...
}

class UserHistoryTO {
 private Long id;
 ...

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }
 ...
}

class UserDetailsTO extends UserTO {
 private List<UserHistoryTO> userHistory;
 ...

 public List<UserHistoryTO> getUserHistory() {
  return userHistory;
 }

 public void setUserHistory(List<UserHistoryTO> userHistory) {
  this.userHistory = userHistory;
 }
 ...
}
Do you see the point? Ok, whatever.

I'll go with MyBatis

You've worked hard and developed a bunch of persistence capable entities, data objects to hold projections, a good amount of JPQL/HQL NamedQueries, a few native SQL queries, optimistic locking is still perfect, you've put OpenEntityManagerInViewFilter because of some lazy things do not adhere to transaction demarcation and boundaries, and it works, it works just fine. Then you push it to production wait a month or so open The Slow Query Log, !#@! you think.

I'd advise against JPA/Hibernate especially when you do something highly scalable and highly available (see Sean Hull's 20 Biggest Bottlenecks That Reduce And Slow Down Scalability, Rule Number 9), especially when CQRS and Event Sourcing are a big deal. Ok, one may believe that doing programming with relational database without knowing how to write SQL and what execute plan will be used to run it is a good idea in case you protect yourself with Hibernate. But despite Hibernate abstracts RDB it doesn't remove complexity, take a look at Hibernate ORM documentation. And after all abstractions are leaky and you'll end up debugging SQL and writing native queries.

But you probably do not want to program in JDBC anymore. To make it convenient to work with DB and let DB do its stuff I'd use MyBatis that just removes boilerplate code and doesn't introduce some piece of magic. And yes, it is simple, see MyBatis3 Introduction.

To make you feel what it is like a really minimalistic example below. Mapper xml file src/main/resources/test/persistence/TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="test.persistence.TestMapper">

 <select id="selectOne" parameterType="long" resultType="test">
  SELECT
  t.id, t.name
  FROM test t
  WHERE t.id = #{id}
 </select>
</mapper>
Mapper interface src/main/java/test/persistence/TestMapper.java
package test.persistence;

import test.model.Test;

public interface TestMapper {
 Test selectOne(Long id);
}
Transfer object src/main/java/test/model/Test.java
package test.model;

import java.io.Serializable;

public class Test implements Serializable {

 private Long id;

 private String name;

 public Test() {
  super();
 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}
And a piece of code that make use of it
try (SqlSession session = sqlSessionFactory.openSession(TransactionIsolationLevel.REPEATABLE_READ)) {
 TestMapper mapper = session.getMapper(TestMapper.class);
 Test res = mapper.selectOne(1L);
 session.commit();
}
So as you may guess the mapper interface is implemented by MyBatis with the help of provided mapper xml file. See MyBatis3 Getting started.

Tuesday, December 24, 2013

Java, ConcurrentSkipListMap

Why there is no ConcurrentTreeMap in java? Because trees are badly parallelized, but it is easy to implement lock-free skip list with the same cost O(log(n)).