What is Reactive Programming?
If a program propagates all the changes that modify its data to all the interested parties (users, other programs, components, and subparts), then this program can be called reactive.
Simple Example:
int a = 4;
int b = 5;
int c = a + b; System.out.println(c); // 9
a = 6; System.out.println(c); // 9 again,
but if 'c' was tracking the changes of 'a' and 'b', // it would've been 6 + 5 = 11; Therefore this is not reactive.
Why should we be reactive?
In the past it was normal for websites to have a slower response time, but in the current day, we have to deal with high response to the user.
eg: "likes" to some content can be reflected in real time to other connected users, and so forth. In most cases, these applications communicate with a large number of remote services as well.
How to be reactive ?
Modular/dynamic - because modules can go offline and come online without breaking or halting the entire system - Being modular means the system should be event-driven. We can divide the system into multiple micro-services/components/modules that are going to communicate with each other using notifications
Scalable: This way, we are going to be able to handle a huge amount of data or large numbers of user requests. - to react to load without falling apart.
NOTE: You can build a message-driven, resilient, scalable, and responsive application without using a reactive library or language.
Reactive Programming With Java?
(https://github.com/ReactiveX/RxJava), an open source Java implementation of the reactive programming paradigm. Writing code using RxJava requires a different kind of thinking, but it will give you the power to create complex logic using simple pieces of well-structured code.
Example:
List<String> list = Arrays.asList("One", "Two", "Three", "Four", "Five");
//RXJava Observable
Observable<String> observable = Observable.from(list);
// Subscribe to the Observable. It will PUSH it's values to the Subscriber, and it //will be printed.
observable.subscribe(new Action1<String>() {
public void call(String element) {
System.out.println(element);
}
}, new Action1<Throwable>() {
public void call(Throwable t) {
System.err.println(t);
}
}, new Action0() {
public void call() {
System.out.println("We've finnished!");
}
});
Here, we can subscribe to the Observable instance. By subscribing, we tell RxJava that we are interested in this Observable instance and want to receive notifications from it. We subscribe using an anonymous class implementing the Action1 interface, by defining a single method—call(T). This method will be called by the Observable instance every time it has a value, ready to be pushed. Always creating new Action1 instances may seem too verbose, but Java 8 solves this verbosity.
So, every string from the source list will be pushed through to the call() method, and it will be printed
This is just an introduction:
Please find here for more info.
https://github.com/meddle0x53/learning-rxjava
Wednesday, December 23, 2015
Sunday, August 23, 2015
Microservices and Spring Boot
A typical architecture would be a WAR file where everything is packaged all in one. This file can be as big as 50 MB to more than 300MB.
Problems with a monolith architecture like this is :
- High Budget,
- Long release cycles
- Difficult to understand a large code base which means it is relatively expensive to change and difficult to scale
The solution for the above is a Microservices architecture, where instead of you building one big large app, you build separate smaller apps, each handling its own business domain.
Benefits of the above architecture is that
- Small budget - Smaller teams needed.
- shorter release cycles
- cheap to change or rewrite, as the apps you work on a sufficiently smaller
- Easier to scale, - depending on high load, only the required modules can be deployed to more servers, unlike in a monolith app, the whole app gets impacted. Therefore in a micro-service architecture you can make use of your infrastructure wisely.
- Faster return on investment as it is easier and faster to deploy smaller apps into production
- Easier to understand a smaller code base.
To achieve the above benefits you need a light weight framework and deployment infrastructure.
Spring Boot makes it easier to create standalone production-grade spring based application that you can simply "just run" , and there is no need to deploy WAR files. The spring boot framework automatically configures a server for you. It has embedded Tomcat or Jetty.
It also has production ready features for monitoring like metrics, health checks and externalized configuration. You can even ssh to your spring boot app and also has easy connectivity to open source PAAS cloud foundry.
For more information: http://projects.spring.io/spring-boot/
Monday, June 29, 2015
ESB Architecture and JBoss ESB
ESB Architecture
How does an ESB differ from an Application Server? Generally an application server will consist of Servlets, EJBS, Webservices, JCA Components (For connecting to databases), and can also have a persistence layer like JPA/Hibernate. Application servers can also be extended to have rules engines like Drools etc. A typical application server architecture would look like:
However, in a typical enterprise, a system may have to deal with multiple other systems, running on multiple clusters and external distributed environments. For example some of these external systems may include a Billing System, an Inventory System, an Accounting System and a CRM, and this is where an ESB Architecture fits in.
An ESB System, provides a set of tools for synchronizing/orchestrating the communication between these external systems. Note: Some of these external systems may not be in Java, and may involve other technologies like .NET, Php/Lamp, or other legacy technologies. Also communicating between these systems may not simply be RMI or JMS Messages in a Java Environment. It may also involve communicating using SOAP/XML messages over a Web service or Restful interface, or simply reading file in different formats over an FTP link.
JBoss ESB
How does an ESB differ from an Application Server? Generally an application server will consist of Servlets, EJBS, Webservices, JCA Components (For connecting to databases), and can also have a persistence layer like JPA/Hibernate. Application servers can also be extended to have rules engines like Drools etc. A typical application server architecture would look like:
However, in a typical enterprise, a system may have to deal with multiple other systems, running on multiple clusters and external distributed environments. For example some of these external systems may include a Billing System, an Inventory System, an Accounting System and a CRM, and this is where an ESB Architecture fits in.
An ESB System, provides a set of tools for synchronizing/orchestrating the communication between these external systems. Note: Some of these external systems may not be in Java, and may involve other technologies like .NET, Php/Lamp, or other legacy technologies. Also communicating between these systems may not simply be RMI or JMS Messages in a Java Environment. It may also involve communicating using SOAP/XML messages over a Web service or Restful interface, or simply reading file in different formats over an FTP link.
JBoss ESB
Sunday, April 26, 2015
Restful Web services with Spring Boot, Java EE 7, Angular JS and MongoDB
During this month of April I was able to experiment with Restful web service
implementations with Java Web Profile 7, Hypermedia-Driven RESTful Web services with Spring Boot, Angular JS, Bootstrap and Mongo DB.
I was able to create two Restful projects.
1) A Java EE 7, MongoDB , Bootstrap CRUD single page web app that uses additional JQuery and Bootstrap plugins in the front end. The sample application and instructions on how to setup the application can be accessed from here
2) A Spring Boot , Mongo DB , Bootstrap CRUD Single page web app that calls Hypermedia based Restful Services. The front end is powered and made more structured with Angular JS. This application source can be accessed from here
implementations with Java Web Profile 7, Hypermedia-Driven RESTful Web services with Spring Boot, Angular JS, Bootstrap and Mongo DB.
I was able to create two Restful projects.
1) A Java EE 7, MongoDB , Bootstrap CRUD single page web app that uses additional JQuery and Bootstrap plugins in the front end. The sample application and instructions on how to setup the application can be accessed from here
2) A Spring Boot , Mongo DB , Bootstrap CRUD Single page web app that calls Hypermedia based Restful Services. The front end is powered and made more structured with Angular JS. This application source can be accessed from here
Sunday, March 22, 2015
How to hot deploy Spring MVC - Spring Boot Application in Intelij
- Create your first Spring MVC / Spring Boot application by cloning the sample project provided in Spring's web site
- In IntelliJ open the Maven project
- mvn clean install
- Right click the Application.java class and simply run it in Intellij.
- Notice that once you access the application from http://localhost:8080/greeting and if you do any changes to the files in src/main/java/resources/templates/greeting.html; those changes do not get reflected unless you do a restart.
- However if you create a webapp folder and have an index.jsp under i.e. src/main/webapp/index.jsp and make changes to that file; then you will be able to dynamically see any changes done to it with a simple page refresh in the browser. Note: Its simply the files under src/main/java/resources/ that do not reflect changes. To overcome this, and to achieve class level and resource level changes to hot deploy ..
- We are going to add the springloaded plugin. Add the plugin dependency as follows:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId><version>1.2.0.RELEASE</version></dependency></dependencies>
</plugin>
- Do a mvn clean install and make sure the jar is downloaded to your m2 repository.
- Next copy the downloaded jar file from the repository to your root folder of your application.
- In Intelij -> Run configuration -> Specify VM arguments :
note: you can also give the path like c:\\myfolder\\springloaded.jar
so it will be like
-Djavaagent:c:\\myfolder\\springloaded.jar -noverify.
- inside src/main/java/resources folder create a new file application.properties. Its contents should have two properties :
spring.thymeleaf.cache=false
- shut down the servers and run the application again. Do class level changes and make changes to the /java/resources/templates/greeting.html . After doing the changes make sure to run MAKE to compile the classes.
- Changes will be reflected immediately in the browser without having to rerun the Application.java.
Friday, February 13, 2015
Mocking and Stubbing and using PowerMock
Unit testing is when developers adhere to a practice of testing units of code. There are many benefits in writing unit tests, specially when it is done in TDD (Test Driven Development) as an agile practice. However in this article I am not going to describe the many benefits of unit testing as that information is already flooding the internet. Even though Martin Fowler, in his article explains the differences between Test Doubles (Mocks, Stubs, Dummy and Fake Objects), in this article I intend to focus on the subtle difference between Stubbing and Mocking.
My understanding is that Mocks and Stubs are there to eliminate testing all the dependencies so that it would be easy for us to focus on the unit of code that we want to test. Which means that these dependencies are either mocked or stubbed.
The main difference between mocking and stubbing to me is that, when Mocking we pre-program behavior. Basically we have the flexibility to tweak the behavior or create our own expectation on how the collaborating classes should behave for every scenario that we want to test.
This means that when mocking, during verification stage we get to perform behavior verification. i.e we ensure that "class under test" would make the correct calls to the collaborating classes,in a fashion that we have defined.
Stubs: are also already pre-programmed code as well; but they have a fixed response. However in Martin Fowler's, assay, it is mentioned that Stubs can also perform behavior verification. They are known as spy objects.
According to this Vedio; basically you can clearly differentiate a Mock from a Stub , is when you know that Mocks do verification of the interaction. Stubs simply are just used for faking.
In most legacy systems, and to my experience I have come across many tightly coupled code with legacy system frameworks. Code that has been so tightly coupled that it sometimes makes the code truly untestable.
I found Powermock a useful library for unit testing, as it has the facility to mock static, and private methods as well. Please checkout my github repository here.
My understanding is that Mocks and Stubs are there to eliminate testing all the dependencies so that it would be easy for us to focus on the unit of code that we want to test. Which means that these dependencies are either mocked or stubbed.
The main difference between mocking and stubbing to me is that, when Mocking we pre-program behavior. Basically we have the flexibility to tweak the behavior or create our own expectation on how the collaborating classes should behave for every scenario that we want to test.
This means that when mocking, during verification stage we get to perform behavior verification. i.e we ensure that "class under test" would make the correct calls to the collaborating classes,in a fashion that we have defined.
Stubs: are also already pre-programmed code as well; but they have a fixed response. However in Martin Fowler's, assay, it is mentioned that Stubs can also perform behavior verification. They are known as spy objects.
According to this Vedio; basically you can clearly differentiate a Mock from a Stub , is when you know that Mocks do verification of the interaction. Stubs simply are just used for faking.
In most legacy systems, and to my experience I have come across many tightly coupled code with legacy system frameworks. Code that has been so tightly coupled that it sometimes makes the code truly untestable.
I found Powermock a useful library for unit testing, as it has the facility to mock static, and private methods as well. Please checkout my github repository here.
Friday, January 23, 2015
Thursday, January 15, 2015
How to setup a custom JaaS Login Module in Glassfish 4.1
A very basic, minimal requirements glassfish4 and JAAS login module can be gotten from :
git clone https://github.com/byorn/glassfish4-jaas
Once cloned open the project with Netbeans 8.1
Please find below the steps to create a basic custom login module in Glassfish 4.1
Step 1: Create the login page: login.jsp
<welcome-file>secure/index.jsp</welcome-file>
</welcome-file-list>
is pointing to this secure page. Once the user tries to call this page from the browser, the system
will prompt the login.jsp page, asking the user to login first.
Notice: the myCustomRealm. This will be mentioned in the next few steps. Basically, later on we create a custom Realm called myCustomRealm, which actually points to JAR file..(The login module) that you are about to create.
Step 3: configure Sun-web.xml or glassfish-web.xml
These entries need to be present in your sun-web.xml. It could be glassfish-web.xml also, depending on how you created your web module in Netbeans.
<security-role-mapping>
<role-name>admin</role-name>
<group-name>admin</group-name>
</security-role-mapping>
Step 4: Build the Login Module
You need to create two files.
1) A LoginModule that extends com.sun.appserv.security.AppservPasswordLoginModule
2) A Realm which extends com.sun.appserv.security.AppservRealm;
In order to get these files, you need to import the following JARS to your project
* glassfish-ee-api.jar
* security.jar
These Jars can be found in : glassfish-4.1\glassfish\modules
1) LoginModule
public class MyLoginModule extends AppservPasswordLoginModule{
public MyLoginModule() {
System.out.println("MyRealm LoginModule - Construction");
}
@Override
protected void authenticateUser() throws LoginException {
System.out.println("Going to Log In ............................");
String userString = _username;
//HERE YOU CAN GET A HANDLE TO A JDBC CONNECTION POOL IN GLASSFISH
//FROM THE JNDI NAME, AND EXECUTE A SQL TO RETRIEVE ALL THE GROUPS
//THE USER BELONGS TO
String[] groups = {"admin"};
commitUserAuthentication(groups);
}
}
In he authenticateUser() method you can get a handle to a JDBC Resource connection pool
and retrieve all the groups the user belows to.
Notice from _username and _password, you can get access to the username and password that was passed from the login page.
2) The Realm Class
public class MyRealm extends AppservRealm{
private static final String JAAS_CONTEXT="jaas-context";
@Override
public void init(Properties properties) throws BadRealmException, NoSuchRealmException {
System.out.println("Init MyRealm");
// Pass the properties declared in the console to the system
String propJaasContext=properties.getProperty(JAAS_CONTEXT);
if (propJaasContext!=null) {
setProperty(JAAS_CONTEXT, propJaasContext);
}
}
@Override
public String getAuthType() {
return "WebAuthorization";
}
@Override
public Enumeration getGroupNames(String string) throws InvalidOperationException, NoSuchUserException {
// return Collections.enumeration(getGroups());
return null;
}
}
Above is the minimal configuration needed.
Note: My glassfish in netbeans is configured to run in domain1. Yours may be different.
In netbeans under services tab -> servers you can delete the galssfish server and add it again.
Here you get to specify your custom domain. If the domain you specify is 'custom_domain' then you need to put the jar file to \custom_domain\lib folder.
Step 6 Edit login.conf
Open login.conf residing in \glassfishinstallation\glassfish\domains\domain1\config
Add the below entry to the end of the file
myloginmoduleRealm {
myloginmodule.MyLoginModule required;
};
Step 7 Configure the security realm in Glassfish Admin portal
Open http://localhost:4848/
In above console, notice domain1,
notice that the security realm myCustomRealm is created under server-config and not under default-config.
git clone https://github.com/byorn/glassfish4-jaas
Once cloned open the project with Netbeans 8.1
Please find below the steps to create a basic custom login module in Glassfish 4.1
Step 1: Create the login page: login.jsp
<form method=post action="j_security_check" >
<span>Username:</span>
<input type="text" name= "j_username" >
<span>Password:</span>
<input type="password" name= "j_password" >
<input type="submit" value="Login" class="btn"/>
</form>
Step 2: Configure Web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>secure</web-resource-name>
<url-pattern>/secure/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>admin</role-name>
</security-role>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>myCustomRealm</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
If you checked out the project source you can see a folder called /secure/index.jsp.
This page can be accessed only if the logged in user has the role 'admin'
Notice that, the welcome page:
<welcome-file-list><welcome-file>secure/index.jsp</welcome-file>
</welcome-file-list>
is pointing to this secure page. Once the user tries to call this page from the browser, the system
will prompt the login.jsp page, asking the user to login first.
Notice: the myCustomRealm. This will be mentioned in the next few steps. Basically, later on we create a custom Realm called myCustomRealm, which actually points to JAR file..(The login module) that you are about to create.
Step 3: configure Sun-web.xml or glassfish-web.xml
These entries need to be present in your sun-web.xml. It could be glassfish-web.xml also, depending on how you created your web module in Netbeans.
<security-role-mapping>
<role-name>admin</role-name>
<group-name>admin</group-name>
</security-role-mapping>
Step 4: Build the Login Module
You need to create two files.
1) A LoginModule that extends com.sun.appserv.security.AppservPasswordLoginModule
2) A Realm which extends com.sun.appserv.security.AppservRealm;
In order to get these files, you need to import the following JARS to your project
* glassfish-ee-api.jar
* security.jar
These Jars can be found in : glassfish-4.1\glassfish\modules
1) LoginModule
public class MyLoginModule extends AppservPasswordLoginModule{
public MyLoginModule() {
System.out.println("MyRealm LoginModule - Construction");
}
@Override
protected void authenticateUser() throws LoginException {
System.out.println("Going to Log In ............................");
String userString = _username;
//HERE YOU CAN GET A HANDLE TO A JDBC CONNECTION POOL IN GLASSFISH
//FROM THE JNDI NAME, AND EXECUTE A SQL TO RETRIEVE ALL THE GROUPS
//THE USER BELONGS TO
String[] groups = {"admin"};
commitUserAuthentication(groups);
}
}
In he authenticateUser() method you can get a handle to a JDBC Resource connection pool
and retrieve all the groups the user belows to.
Notice from _username and _password, you can get access to the username and password that was passed from the login page.
2) The Realm Class
public class MyRealm extends AppservRealm{
private static final String JAAS_CONTEXT="jaas-context";
@Override
public void init(Properties properties) throws BadRealmException, NoSuchRealmException {
System.out.println("Init MyRealm");
// Pass the properties declared in the console to the system
String propJaasContext=properties.getProperty(JAAS_CONTEXT);
if (propJaasContext!=null) {
setProperty(JAAS_CONTEXT, propJaasContext);
}
}
@Override
public String getAuthType() {
return "WebAuthorization";
}
@Override
public Enumeration getGroupNames(String string) throws InvalidOperationException, NoSuchUserException {
// return Collections.enumeration(getGroups());
return null;
}
}
Above is the minimal configuration needed.
Step 5 Deploy the Login Module.
The login module in \glassfish4-jaas\myjaasloginmodule\dis\myjaasloginmodule.jar should be copied to \[glassfish installation] \glassfish\domains\domain1\lib folderNote: My glassfish in netbeans is configured to run in domain1. Yours may be different.
In netbeans under services tab -> servers you can delete the galssfish server and add it again.
Here you get to specify your custom domain. If the domain you specify is 'custom_domain' then you need to put the jar file to \custom_domain\lib folder.
Step 6 Edit login.conf
Open login.conf residing in \glassfishinstallation\glassfish\domains\domain1\config
Add the below entry to the end of the file
myloginmoduleRealm {
myloginmodule.MyLoginModule required;
};
Step 7 Configure the security realm in Glassfish Admin portal
Open http://localhost:4848/
In above console, notice domain1,
notice that the security realm myCustomRealm is created under server-config and not under default-config.
Add property called jaas-context and provide the value to be myloginmoduleRealm which is actually the name mentioned in the login.conf file.
This is pretty much all you need to do. Let me know if you ran into any difficulties.
This is pretty much all you need to do. Let me know if you ran into any difficulties.
Friday, January 2, 2015
Sharing a folder in Ubuntu 14 running on Oracle Virtual Box with Windows 8.1
The below video explains how to create a shared folder in Oracle Virtual Box to share files between Ubuntu14 and Windows 8, where Ubuntu14 is running inside Oracle Virtual Box.
The command to use in Ubuntu is:
sudo mount -t vboxsf virtualbox_shared /home/byorn/mywork
/home/byorn/mywork is the working folder in Ubuntu,
and all the files you create in this folder will be accessible to the shared folder created in Oracle Virtual Box, the shared name given as virtualbox_shared
Subscribe to:
Posts (Atom)