Opublikowany

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

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

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

Interceptor
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; } }

Binding
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
TJ

Autor
Kategorie Java

Opublikowany

Recently for my private project I was forced to use HTTP GET request from my android application to HTTP server.
After some tries with HTTPUrlConnection which is OK but is hard to implement more complicated logic I’ve discovered project OkHttp (http://square.github.io/okhttp/).
It has all I needed out of the box.
Just added Gradle dependency in project build file.
compile «com.squareup.okhttp3:okhttp:3.5.0»

Simple HTTP GET looks like that:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); return response.body().string();

Where:
client – is only one instance which is needed for multiple HTTP GET calls
request – represents current call
url – is the instance of URL class where in my case I had concatenated all parameters as ? … & with Base64 encoding
response – represent response data from the server

What surprised me is out of the box support for 302 response headers and easy cooperation with different servers.

Best regards
TJ

Autor

Opublikowany

Some time ago I read article about Scala and Java comparison (see link link ) and started to think about performance.
Both languages are executed at JVM but are they have the same statistics, for example with simple operations on big arrays?
To check that I prepared simple one class Java and Scala program and executed it on my laptop.
To be honest I was surprised that Java 8 is much more faster than Scala.
The code You could see at my github: Java vs Scala performance comparison code ( link )
The results are presented below:

Maybe Scala is good choice while You think about thread safety but for fast simple business logic processing application java 8 is better.

Best regards
TJ

Autor
Kategorie Java