Friday, 30 December 2016

This week 20/2016

DataNucleus Data Object  is one of JDO implementation. A performance of this implementation is 2 - 10 times slower then Hibernate (link to test).

Apache Isis - supports domain driven design by requirement of preparing model. Other things are generated and shared by configurable wicket GUI. 

EMF - Eclipse Modeling Framework - it is framework to defining and create model of data by eclipse modelling tools and then on basic of model there is generated Java code. 

JDepend - a tool which shows references between packages. Handy tool to check application design.

Classycle - similar tool to JDepend but additionally it allows to check references on class level as well.

Eclipse Metrics plugin - a eclipse plugin to check dependences between classes and cycles in packages.

Hibernate Search - A hibernate solution of full text searching using Apache Lucene engine. In my case I tried standard solution: single instance, standard indexing with result storage in files. I tried to change a language of analysed tokens but I couldn't find Polish dictionary.
Indexing is processed on request and can be executed as a blocking or asynchronous operation.


jMock - another test tool which allows to create mock. I used this tool in a few cases but the same I can do by Mockito. This tool has other order of processing than Mockito (given, when, then). JMock defines mocks and execution listeners then allows to execute tested method. After 4 hour I think, this tool is less flexible then Mockito but require less complex object to test. jMock couldn't create mock of class, it is possible only to mock interface and it is required to mock every used methods. In my cases it required more code and create instance of object - not mocks.

Saturday, 19 November 2016

This week 19/2016

This article is a summary of thoughts about implementing a piece of DDD in my application. This weekend I disinterred an old topic: How can I design something similar to the DDD in my application and don't turn over all existing application? How can I gently get into.
In inherited application there is strongly used hibernate. Entities model looks like it tries to build a full domain model - they have states and behaviours. However all functionalities use entities in view (and Session on view pattern) and it is difficult to do hermetic model especially then all entity attributes has public setters and getters.

My idea were:
  • to retrieves entities only to service level and then map them to immutable dto and dto use in views or if it was necessary map them to mutable Java Plain Object.
  • change access type for data in entitles. Till now there was property access an there was impossible to remove setter method from entity. I prefer field access. It is much more transparent and allow to remove getters and setters which can be implemented by Lombok library.
  • add new layer - application layer which collect a few independent functionalities and share them to controller.
  • I changed packaging of my classes. Classes were included to packages split in order: layer name, functionality. Now I package my classes in order: functionality, layer, but I still thinking about removing layer part of packages and much more granulate functionality.
  • reports contains data from many independent tables. I get data by native sql directly mapped to immutable dto.

Now I was focused on bounded domain model and how to resolve it in my application. In this application I should much more use CQRS pattern.


Anyway I think about some other implementation problems, ex. how should I implement JPA entities of bounded context? One context needs only a few attributes of all model, other needs some other but that attributes are archived in one db table.

I still looking for best solutions:)

Bellow I added a few interesting resources:

ddd series

ddd-in-practice

dddexample

domain-driven-design-with-java-ee-6

ddd-and-spring

Saturday, 12 November 2016

This week 18/2016

When I was at Devoxx 2016 conference, I chose one of DuyHai DOAN's presentation. I was late and I went at 15 minutes. It seemed that he was talking about some DSL framework so I was disappointed and bored. Lucky I tried to watch the presentation one more time and now I know that I didn't understand the context, the first 10 minutes was the most interesting.

However, to the point. Inspirited by DuyHai DOAN's presentation, I have interested in Annotation Processing. I found a good and clear article about this subject and all became easy.
But to the point. what is the Annotation Processing?
It is a phase of Java code compilation to byte code by javac. As you can see at picture bellow


http://openjdk.java.net/groups/compiler/doc/compilation-overview/javac-flow.png
at the beginning of compilation, the code is parsed and there is created a syntax's dictionary. Then there is executed the Annotation Processor which can read Java code or create new one. (There is assumption, the Annotation Processor can't modify existing code but there is some possibility. I will write about it later).
If the Annotation Processor creates new code, all cycle is repeated. If not, all code is analysed and translated to byte code. 

To study the Annotation Processor, I created 3 projects:
  • my annotation 
  • my implementation of the Annotation Processor 
  • some code in which I used my annotation.

Annotation: 
Notice a Retention Policy type.
@Retention(RetentionPolicy.SOURCE)
@Target({ ElementType.TYPE })
public @interface Getter {
}


Annotation Processor:
I created a file "javax.annotation.processing.Processor" in path "META-INF/services" and I put into it a path to implementation of my Annotation Processor 


@SupportedAnnotationTypes("test.annotationprocessor.annotation.Getter")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class GetterProcessor extends AbstractProcessor {

    public GetterProcessor() {
        super();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        createClassWithVelocityTemplate(roundEnv);
        return true;
    }

    public void createClassWithVelocityTemplate(RoundEnvironment roundEnv) {
        Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Getter.class);
        for (Element element : elements) {
            try {
                FileObject in_file = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", element.asType().toString().replace(".", "/") + ".java");

                CharSequence data = in_file.getCharContent(false);
                String data1 = addAFewMethods(data).toString();

                FileObject out_file = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", element.asType().toString().replace(".", "/") + ".java");
                Writer w = out_file.openWriter();
                w.append(data1);

                velocityGeneration(w);
                w.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
....
    }
}
It reads a source of class with my annotation and create new class with additional code.


To be drawing subject to the end, it is good to mention in a nutshell: 

  1. Annotation Processing is supported by IDEs like Eclipse or IntelliJ. It is required to check this option in project settings.
  2. As I mentioned above, the Annotation Processor can read and create new code, however authors of Lombok library have found a bypass to this limit. Their library works as it could change a code. They modifies a syntax's dictionary and inject their code. 
  3. Javac doesn't use JVM but it is possible to debugging code in your IDE. When a debug option is added to execute command, javac shares an interface like JVM for remote connections.

Monday, 31 October 2016

This week 17/2016

SmartParam (v.1.1.2) is a powerful framework to parametrise an application.

1. Source of configuration
It is possible to load configuration from text file, database or create it dynamically.  Framework supports directly H2, MySql and Postgesql databases.


example of configuration file:

{
  name: "simpleCase",  inputLevels: 2,  cachable: true,  nullable: true,  arraySeparator: ";",
  levels: [
    {name: "registrationDate", type: "date",   matcher: "between/ie" },     
    {name: "name",                     type: "string" }, 
    {name: "value",                     type: "integer" }, 
    {name: "value2",                   type: "integer" }
  ]
}
registrationDate;name;value;value2
*: 2013-12-01;Adam;20*: 2013-12-01;Monia;5
 


2. Number of input/output parameters
At the beginning it is required to define structure of parameters:
  • name - name of configuration,
  • inputLevels - count of input parameters, which are used to wind exact output parameters,
  • nullable - by default it is false and if any rule match input parameters, it throws exception
  • levels - defines types of input and output parameters.
3. Supported parameters
Framework supports simple types of data, however it is possible to define your own type. There are two ways to do that: do converter or  type holder.
Type holder ex.
@ParamType("localdate")
public class LocalDateType implements Type<LocalDateHolder> {
.....
}
 
public class LocalDateHolder extends AbstractValueHolder {
    private final LocalDate date;
    public LocalDateHolder(LocalDate date) {
        this.date = date;    }
....
} 

4. Matcher
Framework provides all needed matchers but there is possible to create your own matcher.
ex.
@ParamMatcher("mymatcher")
public class MyMatcher implements Matcher {

    @Override    public <T extends ValueHolder> boolean matches(String s, String s1, Type<T> type) {

        return s.toString().equals(s1.toString());    }
}

One of useful matcher is BetweenMatcher. It supports defining a ranges of dates, numbers and defines default value by *

5. Other Notices
Framework presents itself great but project looks to be extinct.

Saturday, 22 October 2016

This week 16/2016

How works a synchronisation of some object in a nutshell.

My goal is to synchronise a block of code by some string which is a parameter of a function.

At the begging I wasn't sure how synchronisation mechanism works. Is it using equals and hash code to compare object or memory address where object is allocated.
So synchronisation doesn't use equal method but memory address where object is allocated to.

So how to synchronise part of code when I have many instances of the same object (ex. string)?

A solution is to use some container for synchronized objects and retrieves instance of  that object to use it to synchronization. In this case the best choose is map where it is possible to find object by key and get its value or if it not exists yet, add it.

So we need some kind of cache but how to manage this cache? We need only object instance when the synchronisation block is executed.
I think the easiest way on a single jvm is to use a weak reference. In this case I create a cache manager with WeakHashMap. The manager has a synchronized method findOrAdd and returns an instance of string object included to map.
What is important the WeakHashMap use weak reference only for key object so it is required to put as a value a WeakReference instance with my object.

Map map = new WeakHashMap();
map.put(myString, new WeakReference(myString));
Now, if instance of myString hasn't any reference to any other object or is not used to synchronisation, it will be removed from memory so removed from map as well.

Saturday, 8 October 2016

This week 15/2016

Protractor framework.

The Protractor (v4.0.x) is perfect tool to testing Angular JS application. The main advantage is that it transparently supports asynchronous tasks. You don't care of timeout for Angular asynchronous operation, Protractor is synchronising itself for you. Big advantage is that framework can make snapshot of screen and save it to ex. PNG file.

Saturday, 17 September 2016

This week 14/2016

A few weeks ago I started doing a Angular 2 tutorial. Angular2 is extremely different to first beta version. I chose coding in TypeScript which is recommended language. It is possible to coding in JavaScript or Dart as well but it is less popular.

In my case I chose TypeScript which is compiled to JavaScript and then interpreted by browser. Writing in TypeScript is nice for developer with Java grounds.

Angular 2 defines 4 types of objects: Component, Service, Module and Pipe. 

Before the first final release was published, the concept how to bootstrap application had changed. As I remember, first version of Angular2 allows to execute project without using main module. Later it was changed.
My experience is that the most difficult thing is to match collaborative versions of libraries. I spend much time on selecting exact version of libraries to run some easy application.

And one more think. To create and build production version of code I recommend to use angular-cli.



Saturday, 27 August 2016

This week 13/2016

This week I was creating a functionality. An independent service to export large part of data to MS Excel file and part of service which retrieve data from database. It is obvious that xls format can contain about 65 thousand of rows, so I decided to use xlsx format which I thought it is unlimited but about this it will be later. My requirement was to export from database to excel a large set of data and not kill application. 

First of all I focused on the output. The solution was to not use a XSSFWorkbook but SXSSFWorkbook. In my application currently I use old version of Apache POI v3.7 and there isn't implemented SXSSFWorkbook so in this case there is impossible to solve my problem. SXSSFWorkbook is available from v3.8.
However what I could do after I upgrade a libraries? I checked and it is possible to export huge part of data using less then 64MB heap memory.  The SXSSFWorkbook implementation can save simple data in a stream. Process of creating file is split into two phases. In the first phase implementation is saving processed data into temporary file (on linux it is /tmp/.... file). In the second phase temporary xml file is compressed with additional files containing styles and other information into final file.

By the way I found out that xlsx is not unlimited and every sheet can have maximum a little more then one million rows (2^20) and about 16 thousand columns (2^14).

After I had found out how to export large volume of data to xlsx I looking for solution how to retrieve data from database row by row. I'd like to separate input from output service. I created interface of DataProvider and injected there a RowMapper and other types used in NamedParameterStatement's query method but it doesn't work. Finally I used a ScrollableResultSet with Forward option and limitation of retrieved data at once and it works.


Saturday, 20 August 2016

This week 12/2016

I watched a few presentation about jvm and a garbage collection. Today I will write about topics which are new for me.

I didn't know that:...

1. JVM have a lot of parameters (a few hundreds) and some of them are manageable. It is possible to change their state in runtime (about hundred).

2. Bytecode is compiled by JIT into processor code in runtime but there are a few types of compiled code, depending of:
- free memory for compiled code - every platform have other size of memory
- frequency of usage - often used part of code is better optimised then used onece,
- count of processors and cores,
- complication of code's part.

3. Log4J retrieve logged line of code by dumping a stack trace. It is an expensive operation.
4. New one GC called G1 solve a fragmentation of data by splitting memory area into blocks.

Saturday, 6 August 2016

This week 11/2016

This week I was interested in collecting statistic from my application. I have never been doing that and I think a few times, it could be helpful to resolve some problems. Currently there is other motivation - from it depends my half year premium.

Ok, so I have my Java application and what's next?
I can measure ex. how is used cache, heap memory, CPU and etc.
How can I get this information? 
I can log it to file or other adapter or I can serve it by JMX MBean. Logging to file takes hard drive memory and it can be weight. JMX works as JMS service but it's allow to change some application parameters and is light.

Anyway I started from log statistic in file. What I did?

JVM:
Everything about jvm memory and threads is in class ManagementFactory with static methods. Only you have to dump it to log. MBeans are by default registered in JMX Server too.
I didn't find counter of blocked threads and not dead locked, so I prepared my own method to update MBean as below.

        ThreadInfo[] infos = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
        int blocked = 0;
        for(ThreadInfo ti : infos){
            if(Thread.State.BLOCKED.equals(ti.getThreadState())){
                blocked++;
            }
        }
        mbean.setBlocked(blocked);


Hibernate statistic:
I had to add in configuration parameter hibernate.generate_statistics and than I could get Statistics interface and enable collecting statistic in SessionFactory.

statistics = sessionFactory.getStatistics();
statistics.setStatisticsEnabled(true);
All values are available in Statistics objec.

If you need JMX you have to registry Statistic object as MBean in JMX Server.



EHCache statistic
To get EHCache statistics it is required add statistics attribute to every cache container definition.
statistics="true"

 Then it is possible to get statistics from every cache managers.
I noticed when there is a few cache factories, it is needed to set them not shared. Only cache shared with hibernate should be shared. Otherwise I couldn't get to cache manager for hibernate. I saw only one which I defined in spring configuration.

When I had two different configurations, finally two cache managers. I had to share only one which was based on hibernate ehcache file. Others shouldn't be shared otherwise I couldn't get reference to hibernate cache manager.


By the way I will write how to registry your own MBean on your JVM. What you need is to get JMX service and register your MBean. MBean class have to implement interface with postfix MBean. All method included in interface are available from JMX console. Getters presents values, setters change it and other methods can be executed from jconsole. Example code is shown below.

    MyHello mbean = new MyHelloMBean();
    MBeanServer mbs =  ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("com.example:type=Hello");
    mbs.registerMBean(mbean, name);


Saturday, 16 July 2016

This week 10/2016

This weekend I got to know a little Groovy (v2.4). The most important for me was, how to start developing. I opened one tutorial and I did step by step every action. Finally to use Groovy in eclipse I had to add resource for my version (Mars) and install the Groovy tools. The resource provide the older version of Groovy, not supporting dynamic invoke. I didn't find out how is in IntelliJ case. Anyway it is possible to download jars manually or by extension in Maven and then you can start implementing in Groovy.
Using Eclipse version of Groovy I tested if it is possible to use Jdk8 lambda expression in Java method and then execute it in Groove. Test passed, code compiled and executed.
Now let's study Groovy syntax and it semantic.

Saturday, 2 July 2016

This week 9/2016

It pass a month since last post. I had a break because of my holidays and some other important things. Last week I was at Devoxx conference in Cracow. I'd like to write in a nutshell about some main topics.

1. What does Unsafe class and why there is some cry after it will miss?
Unsafe is a class in com.sun package. It was created for internal use of java classes but after some time it is used by common libraries. The Unsafe allow to manual managing memory usage, ex. creating table with greater index than allow int type. GC doesn't involve this part of memory.

2. Jdk8 - collections and lambda's expressions.
There was shown, how works lazy initialization and processing in collections. 
What gives us lambada expressions.

3. Next releases of Java. Architecture plans.
In Java 9 there is planned standardise an API. They plan to remove close 30 classes from com.sun package. The most problematic is Unsafe class. I mentioned about it role before.
Other main changes are:
- array index will be changed to long type,
- a generic class will save its type in bytecode. It will be possible overloading methods with generic collection.

4. Reactive programming - Rx.Java and Hysterix
Another time there was described Hysterix as a simple and excellent framework to maintenance microservices connections.

5. DDD and bounded domain context. One domain model is too huge and complicated to maintenance it. The best idea is to split it into subdomains connected with some context. Nobody shown a real code of that solution. I always asking me, how to do it with hibernate. Is it at all possible?

6. Angular JS 2
I didn't take a part in that sessions. Anyway sessions described how different is AngularJs 2 from AngularJs 1, pointed the way how to start. In a nutshell Angular 2 was totally changed. Now code is writing in typescript and than is compiled to javascript. As it could be noticed, it is totally other language.

7. Groovy - how to start and difference between syntax in Groovy and Java.
At first it is possible to change Java code file to groovy file and compile it. It will be working.  Groove is slower language, so is not recommended to use on production but it is very useful to create tests. It is possible to basic Java syntax and it will be working to. There is huge support for collections. Anyway last version of Groovy don't support lambda expressions from Java 8. But it is possible to code in Groovy and call Java classes and vice versa and then you can use lambda expressions in Java code. All details about language can be find on page http://groovy-lang.org/style-guide.html
One important notice. Sometimes the same code in Groovy could work otherwise than in Java.

8. JUnit 5 - futures


9. How to pass presentations. Good advice given by programmer for programmers.
Sławomir Sobótka was talking about causes of stage fright. He diagnosed that stage fright is caused by too high self-esteen and fright that it could be reduced.


10. Microservices







Saturday, 4 June 2016

This week 8/2016

This week I try to offload a queue of articles I'd like to read. I found some interesting blog about Gradle (v3.x) getting-started-with-gradle. Blog hides you from installig the tool to develop multi-module project. This time I just read multiple articles and I didn't do any examples, so my knowledge is shallow. Anyway I collect here main notices and differences between it and Maven.
In the nutshell the Gradle is much more powerful tool. It left traditional format of a configuration (XML) and took advantages of the closure syntax, so file look much more simple.

In Gradle it is possible to define own tasks and mark dependences - it's memorise me a Makefile. Gradle interprets Groovy language, ex:

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

Other advantage is that it can generate test reports and you can separate reports for unit testing and integration testing.

A configuration file is smaller than pom file.


Sunday, 29 May 2016

This week 7/2016

This week I finished configure fragments in my simple application. As I mentioned that a filed marked as a number field is associated with a String type of model attribute. If you need to associate your field with other time it is required to create your own binder method.

A long time took me to find out how to make unusual binder method for spinner.  In my case, I needed to bind a view action to a model. I used standard spinner listener to update my model but it wasn't a final solution. The best was to create your own binding method with a special annotations contains used in XML parameters. It is possible to bind more than one parameter at once.

ex.

    @BindingAdapter({"bind:enumElements", "bind:selected"})
    public static <T> void spinnerEnumBinnder(Spinner view, Enum<?>[] enumClass, T selected) {
        if (view.getAdapter() == null) {
            loadEnums(view, enumClass);
        }
        .....
    }


And than it is needed to add in layout XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="linkName" type="org.example.ModelObject" />
    </data>

    <LinearLayout android:orientation="vertical" tools:context="org.example.ParamFragment">
        <Spinner
            android:id="@+id/objectType"
            bind:selected="@{linkName.type}"
            bind:enumElements="@{linkName.deviceTypes}"
            android:layout_gravity="center_horizontal|top"
            android:spinnerMode="dropdown" />

    </LinearLayout>
</layout>

Sunday, 22 May 2016

This week 6/2016

Android - to be continued

In my project I used Fragments to could reuse fragments and to improve performance during switching between screens. Managing fragments it is important to care what is added to which object. Ex. I had problems with hiding and showing fragment because I added it to it self. 

Other area explored by me was testing Android application. 
- Unit tests - the same as for desktop/j2ee application tests
- UI test - there is a tool called uiautomatentest - I haven't used it yet. It can send to Android application events as touches, finger moves etc.

At the end I collected some best practices to develop in Android:
1) don't execute long process task in UI thread - since Android 3.x some IO tasks will throw an exception then they are used in the same thread as UI.
2) prefer to use a service than an asyncTask. During memory cleaning at first are killed asyncTask and only when there is too little memory services are killed.
3) give your application permission only it needs. Application will be safer.
4) store application and user data on phone, avoid save it on external memory.
Now, all hands to the pumps!

Saturday, 14 May 2016

This week 5/2016

Android - another meeting.

This is my another meeting with the Android development (this time with SDK v19). Last time I was coding for Android 2.3. It was 3-4 years ago.
At the beginning I checked what changed in architecture and developing Android applications. From my point of view there isn't any differences. The same as 3 years ago main activity extends Activity object. This time in my project I used fragments for fluent navigation and reuse some parts of code in a activity.

The Android promotes to use free of charge based on JetBrain product IntelliJ Idea - Android Studio IDE. Tool has good Gradle support.

I found one useful library to bind data between view and activity/model. It is possible to bind string object but also it is possible to create your own data converter and use it in layout xml. During creation of binding I founds some problems as:
1) from model I couldn't execute refreshing only one element of a data model
2) binding didn't worked until I used in xml following construction of calling variable @={pojoObjectAliasFromTagData.attribute} - on Android page is missing equal sign.

Wednesday, 4 May 2016

This week 4/2016

Last two weeks I explored Google looking for some libs to support IP camera in a linux environment. I found two or there libs  but any so good to describe some of them.
The main problem is, they don't support RTSP which is the most important for me. I'd like to allow my application two ways communication as it is possible in the Windows version of attached application. I found one native libs dedicated for Android but it doesn't work with my linux kernel.
Request of snapshot or fetch MJPG stream isn't enough for me.
Anyway I have other question. Is it possible to tunnel RTSP stream in https SSL connection?
Nowadays I know that best way to see a steam from my camera is to connect with it by VPN and use VLC to watch video.

Tuesday, 19 April 2016

This week 3/2016

It is my first meeting with Selenium. I didn't know how to start, finally I watched and read a few tutorials. Than I installed the Selenium plugin for Firefox. I was looking for a plugin for Chrome but one plugin I found isn't so useful as this in Firefox.
Than I recorded my first test and exported to a Java class. Hm.. I found six different options of exporting my record to Java code. I can choose one of a few frameworks and type of testing path - local or remote. The plugin allow to  generate Java code dedicated for old Selenium libraries bellow 3 version (it is deprecated code in version 2.52) and developing type for future 3rd version of  Selenium libraries (WebDriver).

I tried all possible combinations except dedicated for TestNG framework.
Finally I can compare some common functionality. Below I  present main difference.
Old SeleniumWebDriver
Selenium interface is deprecated and will be removed in 3.x version
Supports regular expressions in disabled native mode/td>Till now I didn't find any way to localise elements by regular expressions by xpath
All actions are accessible by one interfaceFunctionality of old interface is available by a few separated classes


Next my question was, how can I execute a test generated by IDE. Is it possible to run it from maven and what I need to run.
The IDE generated me a simple test class, in which I updated example package path. Class can be executed as unit test. However it is required to have run test environment for pass a test. I found no other way to do it.

Monday, 4 April 2016

This week 1/2016

Today I'd like to get to know what is done in subject of creating unit test for pl/SQL scripts. 4 years ago I created some unit test for my code. I didn't use any framework or tools. It was just a other package with tests.

However I am writing what new I was learned... Today I read article about how to write unit test in Oracle SQL Developer. This part of development has it own tools, which support preparing and rollback data. Collects statistics an probably much more. However writing unit test in pl/SQL is something unusual and most developers don't do that. Even PL/SQL developers.

Second subject is testing AngularJs UI. As I checked it is possible to test UI with ProtractoJS. Protractor can execute connection and even do snapshot. I haven't tried yet but it looks promising.