What is JMS? Java Message Service could be considered as high level framework that communicates two or more clients each other by sending messages. JMS is fully supported by Java EE and has been specified in JSR -914 (see links section).
Base operations that we could done via JMS are:
- send messages
- receive messages
Typically one JMS client create message and send it to the server (JMS message broker) and other JMS client receive it and perform some operation. Of course this is high level overview and lower process is more complicated while added i.e. security and so on.

Where we use it? Well we could use JMS everywhere where we need increase scalability, integrate different environments, batch processing support, asynchronous communication without time pressure.
Common use is email sending process where one client create message that templates email should be send to appropriate receiver/s and send message to message broker where it is queued and receivers can download them when connected.

What are the key players on the JMS market? Below You could find my private favorite JMS vendors list:
- Amazon SQS
- ActiveMQ
- Oracle Weblogic
- HornetQ (perv. JBoss messaging) see link no 2

What is activemq? ActiveMQ is implementation of Message Oriented Middleware which is used to communicate ie via JMS clients. As we could read at official page (see link no 3) it is open source system that is fast, supports cross language clients and protocols and what’s the most important supporting fully JMS 1.1 specification.
Latest version is 5.x.x and this version will be used in demo.

Active mq server From the download section of official ActiveMQ page (see link no 3) You can download latest server binary package. After un-zip it took almost 100MB of disk space.
Unzipped binaries is all You need to start Your server.
At my windows machine starting script is located at: apache-activemq-5.x.x\bin. I use activemq.bat but there ale similar scripts for Linux too.
Bat script setup local env and finally run jar file called activemq.jar which is located at bin folder.

After run in command line starting script and successfully started server You should see something similar to:

Now You could type http://localhost:8161/admin/ to access Your ActiveMQ server administration console, note that default login and password is admin.

It should look like that:

Now You could add Your first queue in Queues menu.

ActiveMQ client example I created sample client for ActiveMQ server in Java 8 using threads and lambdas. Whole code You could find at my github: https://github.com/tjancz/jms-activemq-example.git

Below You could see sender code:

and the receiver code:

If You would like to see all working stuff just form my github repo.
There is also great documentation on ActiveMQ page.

Summary JMS is not scary as it looks and ActiveMQ is great MOM that could easily help You set up message queues and integrate different systems in MQ way.

1. https://jcp.org/aboutJava/communityprocess/final/jsr914/index.html
2. http://hornetq.jboss.org/
3. http://activemq.apache.org/



REST become web service standard some time ago. To be honest I do not believe that we need SOAP any more since all needed communication functionalities could be done in REST. Probably there are some cases when SOAP web services fits better than REST web services, but generally when I need web services I start with REST.

One of old but still efficient framework for creation of REST web services is Jersey [ Link ] .
So what is Jersey? From the documentation we could see:
‘Jersey is a Java based framework that help java developers creating RESTful Web services with variety of media types and abstract away the low-level details of the client – server communication.’ Well that sounds good for me. I treat it as framework for helping expose REST without many complicated configuration features. That’s really important if You would like to create something universal and easy to understand without additional useless technology stack.

One of nice features that convince me to use Jersey is easy integration with Jetty server.

Basic integration looks like that

public static void main(final String… args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

Server server = new Server(8080); //server.setHandler(new IndexHandler()); server.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, „/*”); jerseyServlet.setInitOrder(0); jerseyServlet.setInitParameter(„jersey.config.server.provider.classnames”, SampleResource.class.getCanonicalName()); server.start(); server.join(); }

For my project [ GitHub Link ] I used dependencies

def versions = [
        «jersey»: ’2.22.2»,
        «jetty» : ’9.3.7.v20160115»

repositories { mavenCentral()

dependencies { compile «javax.ws.rs:javax.ws.rs-api:2.0.1» compile «javax.servlet:javax.servlet-api:3.1.0»

compile „org.eclipse.jetty:jetty-server:${versions.jetty}” compile „org.eclipse.jetty:jetty-servlet:${versions.jetty}” compile „org.eclipse.jetty:jetty-servlets:${versions.jetty}” compile „org.glassfish.jersey.core:jersey-server:${versions.jersey}” compile „org.glassfish.jersey.core:jersey-client:${versions.jersey}” compile „org.glassfish.jersey.media:jersey-media-json-jackson:${versions.jersey}” compile „org.glassfish.jersey.containers:jersey-container-servlet:${versions.jersey}” testCompile group: «junit», name: «junit», version: «4.11» runtime «javax.servlet:javax.servlet-api:3.0.1» runtime «javax.ws.rs:javax.ws.rs-api:2.0.1» runtime „org.glassfish.jersey.core:jersey-client:${versions.jersey}” runtime „org.glassfish.jersey.core:jersey-server:${versions.jersey}” runtime „org.glassfish.jersey.containers:jersey-container-servlet:${versions.jersey}” runtime „org.glassfish.jersey.media:jersey-media-json-jackson:${versions.jersey}”


Please remember about runtime section, especial servlet api is needed.

What really nice is that Jersey is still developed and last release was in Feb 2017.

In my example You could navigate to and see


Kategorie DSP2017, Java


I’ve interested in Jetty web server couple years ago,
it has nice slogan „Don’t deploy your application in Jetty, deploy Jetty in your application!”.

I know that Spring Boot is fancy nowadays, but do we really need to create own stuff in fancy technologies?
Maybe we should choose good solution instead easy one?
Well I’m not telling that Spring Boot is not good, in fact I like it and use it sometimes, but it not fit to all cases.

OK if You are lazy and approve Spring stack You can use Spring Boot to all cases ;)
But I’m not and would like to have small ride with Jetty 9.

Let’s create hello world using that server.

What is Jetty, all should know – it is embedded web server which enable all including HTTP 2 capabilities to application.
Where could I get it – of course from maven central repository. I’m using Gradle and 9.4.x version in this hello ride.

compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.4.2.v20170220'

Basic setup is quite easy

public static void main(final String... args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new IndexHandler());

Just create jetty server object in main method and start it. Interesting thing is being done in setHandler method.
Well first we need to explain what really handlers are in Jetty?

I treat handlers as methods for operating on requests to produce response. Simple nothing more.

So if I would like to present hello world application above IndexHandler would look like that
public class IndexHandler extends AbstractHandler {
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=utf-8");
        final PrintWriter out = response.getWriter();
        out.println("Hello from Jetty 9");

Form the official Jetty documentation [ Link ] passed parameters are:

  • baseRequest – the Jetty mutable request object, which is always unwrapped.
  • request – the immutable request object, which may have been wrapped by a filter or servlet.
  • response – the response, which may have been wrapped by a filter or servlet.

So for sure Spring Boot Hello World would be faster and containing less code, but Jetty 9 give You more freedom of choosing technology stack.

Kategorie DSP2017, Java



Jakiś czas temu wpadłem na stronę konkursu „Daj się poznać”, fajna sprawa od jakiegoś czasu prowadzę bloga i stwierdziłem że może dzięki tej inicjatywie uda mi się bardziej systematycznie dodawać do niego treści.
Wielkie dzięki dla organizatora :+1: :D

Jednak nie o tym ma być dzisiejszy wpis.
W ramach konkursu jak i na użytek własny stworzyłem projekt open source pod zacnym tytułem own phone private backup [ Github: https://github.com/tjancz/ownPhonePrivateBackup ]. Idea jest prosta – robić backup istotnych rzeczy zapisanych na własnym telefonie.
Często łapię się na tym że kasuję wątki sms/mms, zdjęcia, pobrane pliki które ktoś mi wysłał bez zastanowienia, a później jednak okazuje się że były potrzebne. :)
Więc czemu by ich nie wrzucać na własny serwer i trzymać pewien czas zanim znikną bezpowrotnie?
Więc będąc zmotywowany do samorozwoju, mając cel przed oczyma zabieram się do pracy.

Analiza problemu

Pierwszym krokiem który każdy, hmm – „programista” powinien zrobić to przeanalizować problem.
Więc i ja to zrobię.
Cel Główny: Mieć backup istotnych danych zapisanych w moim ukochanym androidzie.
- Backup musi być absolutnie prywatny, żadnych publicznych chmur!
- Urządzeniem które backupuje jest mój android phone
- Serwer na który robię backup musi mieć stos technologiczny (fajne słowo) Javy – bo kocham Javę i już :D
- Backup musi się wykonywać w tle samoistnie ( inaczej już po tygodniu zapomnę by go robić )
- Dane z serwera ulatniają się po określonym w konfiguracji terminie

I to chyba wszystko jak na początek.

Fajnie wiem co chcę zrobić teraz wystarczy wymyślić jak i tu pojawia się magiczne słowo


Jak dinozaur od razu pomyślałem: „Dla tego problemu idealnie sprawdzi się Klient-Serwer.”
Czemu, gdyż projekt w swym założeniu ma tworzyć backup jednego urządzenia w jednym miejscu.
Jednak czym naprawdę jest architektura klient-serwer?
Wikipedia [ https://en.wikipedia.org/wiki/Client%E2%80%93server_model ] definiuje architekturę klient serwer jako klienta który żąda zasobów i serwer który owe zasoby udostępnia.
Hmm i tu strzał w stopę w moim przypadku to serwer żąda zasobów od klienta, chyba że klient jest serwerem a serwer klientem (?) ;)
A może jak to zwykle w życiu bywa rzeczywistość jest bardziej skomplikowana?

Na pewno mam do czynienia z dwoma modułami, aplikacją androida – moduł 1 i aplikacją Java – moduł 2.

Analizując pobieżnie funkcjonalności mogę stwierdzić że
moduł 1:
- wysyła dane do modułu 2
- żąda odpowiedzi czy dane przechowywane są aktualne
moduł 2:
- pobiera dane
- odpowiada informacją o aktualności danych

Wygląda to trochę jakby jednak oba moduły były czasem klientami a czasem serwerami. Natomiast istotą jest tu kwestia inicjatora operacji. To moduł 1 inicjuje żądania a moduł 2 wykonuje na rzecz modułu 1 operacje i dostarcza wyniki.
Nawet jeśli przeanalizujemy przypadek wysyłania backupu to jednak moduł 1 inicjuje wysyłkę danych oraz oczekuję informacji zwrotnej o powodzeniu ich zapisu po stronie modułu 2.

Czyli jednak mam do czynienia z Client – Server :)

Ciąg dalszy nastąpi…


Kategorie DSP2017


Google Guice is commonly perceived as more light solution that Spring. That’s why some applications which do not want to have Spring / Spring Boot use it.
More about guice could be found at guice wiki page – https://github.com/google/guice/wiki
One of the cool features that guice provide to developers are interceptors. They are officially called AOP in Guice documentation, which in fact is true You can create aspects oriented programming using interceptors in guice.

Base approach contains following steps
Creation of annotation to mark method that we would like to intercept
Mark method with annotation
Creation of method interceptor
Binding interceptor in module

So if we would like to create interceptor that logs method execution time it should look like that

Retention(RetentionPolicy.RUNTIME) Target(ElementType.METHOD)
public @interface FullyLogged {

Method mark
@ FullyLogged
public void someMethod() { … }

public class LoggingInterceptor implements MethodInterceptor { private final static Logger LOGGER = LoggerFactory.getLogger(LoggingInterceptor.class);

@Override public Object invoke(final MethodInvocation invocation) throws Throwable { final Stopwatch stopwatch = Stopwatch.createStarted(); final Object returnedObject = invocation.proceed(); LOGGER.info(„Response time from method: „ + invocation.getMethod().getName() + „ of class: „ + invocation.getMethod().getDeclaringClass().getName() +” is: „ + stopwatch.stop().toString()); return returnedObject; } }

public class MyModule extends AbstractModule {

bindInterceptor(Matchers.any(), Matchers.annotatedWith(FullyLogged.class), new LoggingInterceptor()); }

In my opinion interceptors could make developers life easier but like all AOP should be used wise.
Typical for security or logging.

Best regards

Kategorie Java