IntelliJ Settings with High DPI monitors

One day recently, I finally switched from Eclipse to IntelliJ and Lord’o Lord, I love it.

Now, before going any further, let me tell you that it was not easy! There was a bit of learning curve. There were tweaks here and there and I had to remap quite a few ( about 10-15) shortcuts to my choice. And yes, that meant re-learning the shortcuts a little bit. It took me about 2 weeks of full time development to get accustomed to it and now I am about 81%(why odd number? because there is no formula for it 😛 )  comfortable with it.

Now, I am fortunate to have good monitors at home and at work so with power comes responsibility problems! One of the main problems is the size of the menu items! Honestly, I pretended that it did not bother me much, but today I was vexed and had to figure it out, and put it out on the internet because I know for sure that there is a sad soul who is banging their head on the same problem.

Without further ado.. when the size of menu items become so small that even ants cant look at them and you are sitting literally half an inch away from your monitor, follow this step.

I got this from here. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000556364-IntelliJ-magnifies-after-computer-sleeps?page=1#community_comment_115000479810

That’s it.

Happy Coding everyone!

Using properties from Jboss configuration in Java.

Honestly, I am a little embarrassed that I did not know this.

However, here is the problem statement.
Use properties defined in the Jboss configuration file ( standalone.xml or domain.xml) in Java.

The properties are defined in the config file as such ( standalone.xml in my case)

<system-properties>
  <property name="APP-NAME" value="MyApp"/>
  <property name="LOG_DIR" value="C:/Logs"/>
</system-properties>

We are all we aware of the fact that properties are defined in an properties file and we can use it in our Java code (in case if you need help with it, please check this post) but to read the properties defined in Jboss config, we will have to do something like this.

String logDir = System.getProperty("LOG_DIR");
String appName = System.getProperty("APP-NAME");

And that’s it. Pretty simple and straight forward.
Now, if for any reason, you have to append properties, do this.
Example: The logs should remain in C:/Logs/MyApp, so apparently, I have append the properties with a “/”. But now the problem is basing on the OS, this separator might differ. In such situations, File.separator comes to rescue as shown below.

String logDir = System.getProperty("LOG_DIR");
String appName = System.getProperty("APP-NAME");
String log = logDir+File.separator+appName

Hope this helps.

Apache Camel – Calling an Endpoint from a Processor

Like most of my posts, this one was a hard find too. Without further ado, let’s dive in.

This was my requirement. Send the data to different endpoints from my source. I could  have simply done this:

from(fromLocation)
  .to(endpoint1).to(endpoint2);

or if I had to send the same data to multiple endpoints

from(fromLocation)
multicast().to("endpoint1","endpoint2");

But my problem is little complicated. I get the data in a List and each of the element in the List has to be delivered to multiple endpoints. So my processor looked like this:

@Override
 public void process(Exchange exchange) throws Exception {

 List
<map> responseList = (List
<map>) exchange.getProperty("responseList");
 
 for(Map response:responseList){
      String responseMsg = (String)response.get("response");
      String responseType = (String)response.get("responseType");
      if(responseType.equals("XYZ")){
           //send message somewhere.
       }
   }
 }</map></map>

Now, how should I send the message somewhere is the real question. After much struggle, I found the solution in ProducerTemplate.

@Autowired
CamelContextManager camelContextManager;

private ProducerTemplate producerTemplate;

@Override
public void process(Exchange exchange) throws Exception {
  List
<map> responseList = (List
<map>)exchange.getProperty("responseList");
  for(Map response:responseList){
	
      String responseMsg = (String)response.get("response");
      String responseType = (String)response.get("responseType");
      if(responseType.equals("XYZ")){
           producerTemplate =camelContextManager.getCamelContext().createProducerTemplate();
	   producerTemplate.send("file:C:\\out", exchange);
       }
	
}</map></map>

And if you are wondering what my CamelContextManager looks like:

@Component
public class CamelContextManager implements CamelContextAware{

	protected CamelContext camelContext;
	
	@Override
	public void setCamelContext(CamelContext camelContext) {
		this.camelContext = camelContext;
	}

	@Override
	public CamelContext getCamelContext() {
		return camelContext;
	}
	

}

So there you go, pretty straight forward. Now if you have to send it another processor for example, you can simply do:

@Autowired
Processor2 processor2;

public void process(Exchange exchange) throws Exception {
......
      if(responseType.equals("XYZ")){
           processor2.process(exchange)
       }
....
}

Obviously I am using Spring container here, but this is the general idea.
Happy Coding!

Spring Boot + Cassandra – Issues and remedies.

Hi peeps. Looks like Spring Boot and Apache Cassandra do not mingle as easily as they should. After a lot of trials, I finally figured it and once you figure it out, usually the setup is fairly simple.

First you add this to your pom.

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-cassandra</artifactId>
 </dependency>


Second you create a POJO.

import org.springframework.data.cassandra.mapping.Table;

@Table("Users")
public class UsersPOJO {

    private String userId;
    private String name;
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Next, you create a DAO.

import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.stereotype.Repository;

import UsersPOJO;

@Repository
public interface UsersDAO extends CassandraRepository{
    
}

Finally, ofcourse you add a service to add/get data.

import java.sql.Timestamp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserService {

    @Autowired
    private UsersDAO usersDAO;
    
    @RequestMapping(value="/create", method= RequestMethod.POST)
    public String createProfile(){
        
        UsersPOJO usersPOJO = new UsersPOJO();
        usersPOJO.setUserId("abc123");            
        usersPOJO.setName("Alphanumeric");        
        usersDAO.save(usersPOJO);
    
        return usersDAO.toString();
    }
       
}

Now, you should be able to fire up your Spring boot app and insert the data and everything should be fine and dandy until you hit this roadblock!

com.datastax.driver.core.exceptions.InvalidQueryException: 
No keyspace has been specified. USE a keyspace, or explicitly specify 
keyspace.tablename

After much pain and time, I found the solution to this problem. Add the following to your application. properties.

spring.data.cassandra.port=9042
spring.data.cassandra.keyspace-name=Test
spring.data.cassandra.contact-points=localhost

I also added a Configuration file below just to make sure I have enough control over the cluster. However, this step is optional.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

@Configuration
@EnableCassandraRepositories(basePackages = {"com.test"})
public class CassandraConfig {
    @Autowired
    private Environment environment;
    private static final Logger LOGGER = LoggerFactory.getLogger(CassandraConfig.class);
    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(environment.getProperty("spring.data.cassandra.contact-points"));
        cluster.setPort(Integer.parseInt(environment.getProperty("spring.data.cassandra.port")));
        return cluster;
    }
    @Bean
    public CassandraMappingContext mappingContext() {
        return new BasicCassandraMappingContext();
    }
    @Bean
    public CassandraConverter converter() {
        return new MappingCassandraConverter(mappingContext());
    }
    @Bean
    public CassandraSessionFactoryBean session() throws Exception {
        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(environment.getProperty("spring.data.cassandra.keyspace-name"));
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);
        return session;
    }
    @Bean
    public CassandraOperations cassandraTemplate() throws Exception {
        return new CassandraTemplate(session().getObject());
    }
}

4 easy steps for setting up Cassandra DB in Mac.

I, like millions of people in this new millennia, believe in technology. And whenever a cool thing comes out, it immediately excites me. I start reading about it and often times I do get sold quite easily. And due to this reason, I do setup a lot of things on my machine and this is the process I loathe.

And my experience with Cassandra not a piece of cake either! And now that I think about it, it is all my fault. Here below, I will show how ridiculously simple is the setup of Cassandra in a Mac machine. ( It should be similar in the Windows world too but I will write a post on it as soon as I setup one.)

So, here are the steps.

  1. Download.
    • Go to http://cassandra.apache.org/download. There should be a “Latest Version” section in the page here. Check out the latest version by clicking on the version name.  As of date, the latest release is 3.9 and when I clicked the link, it took me to http://www.apache.org/dyn/closer.lua/cassandra/3.9/apache-cassandra-3.9-bin.tar.gz
    • Choose any mirror site and download the tar ball.
    • Untar it any location you prefer.
    • For example sake let’s pretend you downloaded it to the desktop
  2. Give Permissions.
    • Using either terminal or the finder, go to desktop and give Read&Write permissions to all users and staff.
  3. Run
    • Now open up a terminal, navigate to the bin folder.
    • Type ./cassandra
    • After a couple of seconds, your Cassandra DB is up and running.
  4. Open a new terminal tab and type the following the bin folder location.
    • ./cqlsh
    • This command will open up the cqlsh prompt where you will be able to run your CQL commands

 

Accessing Properties files in Spring.

Today, I literally spent over 3 hours to find the easiest way to access a properties file in a Spring project. What I am surprised is, with all the so much documentation about Spring, it was not an easy find. I hope someone will find this and save their time.

The process is very simple and could be achieved in 2 straight forward steps.

1.Create a properties file in

src/main/resources.

Let us call it

test.properties

2.Put this line in your code.

Properties props = PropertiesLoaderUtils.loadAllProperties("test.properties");

That’s it. Now you can use these properties as you wish.

eg., System.out.println(props.getProperty("username"));

Now, remember, you need this line too!

import org.springframework.core.io.support.PropertiesLoaderUtils;

Enjoy!

Building a .war file using maven.

This post will be short and sweet. I hope this helps someone.
Recently I had an interesting requirement. I developed a maven project which was building into a jar. After working many hours on the code, I was on cloud nine that everything was working great. Then I had a meeting with my Infrastructure team. I was told “very politely”, that they will not be able to deploy a jar file and will definitely need a .war file.

Since I was using maven, I was gleaming inside that this will be a two minute change. I opened up my eclipse and my pom clearly shows the packaging types.
Without hesitation, I switched from jar to war and ran the build. I was psyched up until the build failed within a second.

With a quite a bit of research, I found this beautiful plugin.

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

 
Once I plug it in the pom, the war was build beautifully. Oh the world is so blissful!

Getting started on JS Frameworks

This post is especially on a request. The main aim of this post is to help understand people on what’s what in the world of JS and make an informed decision if this arena is a good choice.

I personally think the JS Frameworks are here to stay and they will keep on prospering and progressing in future at least for the next 10-15 years. It will be a long time when there will be something else that will make JS frameworks obsolete. The explosion of JS Frameworks began after the introduction of NodeJS,the glorious JSRE ( Javascript Runtime Environment). It would be surprising to learn that NodeJS was initially introduced in 2009. So,for the past 7 years, many improvements from the open source community has honed it in several ways and recently it has become of the popular demands of the job market.

Before we jump into making decisions on the frameworks,let’s first understand what NodeJS is about. As I mentioned above and in my other post, NodeJS is just an JSRE. Ryan Dahl is the original author of this platform and the wiki on Node is a worth read. Prior to NodeJS, Javascript was used as a front end technology. It was used on the browsers to perform several operations. These included from making the web pages fancy to the introduction of wonderful world of AJAX. Since browsers were needed to render the Javascript, it is a no wonder that browsers have some kind of Javascript engine within them that help run the JS. Such a JS engine on the very popular browser, Google Chrome is called by the name V8.

V8, is top of the line,crème de la crème of Javascript engines and what the authors of NodeJs did is, they extracted this V8 from the browser and with some wrapping code of C++ around it, they were able to concoct a standalone Javascript engine and thus NodeJS was born. So NodeJS, is a standalone JSRE, which could be used just like any programming language like Java,Python,Ruby etc,. There are several advantages of NodeJs over other programming languages but that is out of the scope of this post.

When Ryan Dahl introduced NodeJS in 2009 at the JS Conf, it received a standing ovation. And just like any popular software language, it attracted lot of developers who in turn started writing frameworks around NodeJs. To my knowledge, the major frameworks that came out this sequence of events were MEAN, Meteor, Sails, Koa etc., Of course there are lot more such frameworks and you could do your due diligence before arriving at one, the most popular that are popular today are MEAN and Meteor.

MEAN: MEAN is a full stack framework which stands for Mongodb, ExpressJS, AngularJS and NodeJS. Using this framework, full stack applications could be developed with Mongodb as the database, Express for the web app framework and Angular on the front end and NodeJs for the backend.

Meteor: Meteor is also a full stack framework with NodeJS as its backend and I suggest you to read my post to get little more insight.

What I found personally is, both Meteor and MEAN are designed to be built on MongoDB. Although I don’t have anything against MongoDB, I somehow am not comfortable with the fact that it these frameworks are not database agnostic. Again, this is just my personal opinion and honestly there are about a million apps that are effectively using these frameworks so this formula apparently is a good one.

I have heard from many of my talented friends and colleagues that overall NodeJs and its frameworks are super awesome and they love working on it. So, what does it take a person to start working on either MEAN or Meteor framework? Here are some guidelines that you should consider.

  1. Do a little due diligence on NodeJS, I mean core NodeJS without the framework. Try to understand the inner workings of the language. Just to get you started, here is how to install it. I wrote it for Mac but it is equally simple and straight steps for Windows. Oh btw, if you are installing on Windows, then choose msi.
  2. Because both the frameworks depend on MongoDB, due diligence on MongoDB is also essential. Again, to install MongoDB, you can read this.
  3. Try to get webstorm ( or intellij). This is the best ide for JS development and even if you have to shed a little money on it, it is worth it. Tip: Try to see if you can get hold of a friend who is school. With a school email id, this IDE is free.Shake up all your contacts and find a student who can help you.
  4. Decide on a project. I am a strong strong believer in this. Think up of a project in your head and start working on using the above technologies.Without a project in mind, all the effort will be in vain and the satisfaction of learning something good can never be achieved.

Good luck.

Thoughts on JS Frameworks.

Too many fancy words for one day! Frankly over last year, I have been hearing a lot about the JS frameworks and honestly they were intimidating. Today I grabbed up all my courage and plunged into this JS world. Being a Java developer at heart, I do not have a good impression on Javascript. I do not know how this feeling came into my head but it did.  But, this is in fact the very characteristic which a software engineer should not have. I understand that technology is supposed to progress and engineers should embrace it with all the love and here I am.

One thing that bothered me( and still does) is the plethora of options in JS. Even from the days when I started with jQuery, there were too many names in JS World. Names like AngularJS,MeteorJS, NodeJS, ReactJS, MEAN stack etc etc.. are confusing. So I started with everything.( ofcourse not everything, but I started with some of the big players). When I say “started”, I literally “started” and I did NOT dig deep at all. I barely scratched the surface, just enough to make me familiarize of what’s what.

So here are my findings:

  1. AngularJS is for the front-end. It is much more approachable way than the standard libraries like jQuery.
  2. NodeJS is for the back-end. Simply put, NodeJS is JSRE ( Javascript Runtime Environment) with which you can build server side applications. It is the Javascript without the browser which you can run as a server on your machines.
  3. MEAN stack is basically MongoDB, ExpressJS, AngularJS and NodeJS.
  4. MeteorJS is a full fledged NodeJS framework with which you can build a complete app with front end and back end.

In my research, I read this amazing post https://wiki.dandascalescu.com/essays/meteor_js_vs_the_mean_stack which made me look into MeteorJS. So far am liking it as I am able to scoop up a sample app from meteor in matter of minutes. I think I will be working on it for few days to come and posting on it heavily. So please come back and check them out.

PS: If you are like me and trying to swim through JS frameworks, drop me a word about what you are interested in and why. It will be good to know on what I am missing.

 

Installing NodeJS on Mac.

I know, I know, there are lot of sources out there for this topic, but I still want to write this as a note to myself. Makes it easier to search 🙂

First thing I want to be clear is, I spent a little while looking for this and honestly the best resource is by people at NodeJs. So save time and end your search at https://nodejs.org/en/download/

I chose the .pkg installer and it will open up this window.

Screen Shot 2016-06-19 at 9.45.46 PM

From this step, it is pretty much click, click, click. Once this is done, just do a sanity check. Open up a terminal and type node -v. You should see some version in there.

Screen Shot 2016-06-19 at 9.49.26 PM.png

Now, to satiate the curiosity, let’s run a javascript command at terminal. First thing to fire up the Javascript engine on your machine is to hit node and then blast away. So I ran a couple of very simple commands and they look like this.

Screen Shot 2016-06-19 at 9.54.24 PM.png

That’s it folks. You have successfully installed a Javascript engine on your machine. Now you can use Javascript without the browser and use it as a backend technology! Enjoy!