Author Archives: ChandanPandey

Why web sockets fail on http while succeed over https


I was in a mess and this is the blog which gave me clue out of it.
I had created two simple examples of websocket implementation using yet to realease spring 4 support for websockets.
I tested it from home and all well and good (no issues).
Next day I tried to demo it in my team –and the HTTP version failed, but to surprise the HTTPS was still good and saved my day!
I realized that I have not done my homework properly –rather impressed by simplicity of spring, have rushed in implementation. While searching for the issue I stumbled on the above article –it turns out that “Automatic Proxy” at office network was culprit. Here is the story

  1. We have “automatic proxy ” at work place –meaning no explicit IP or script in proxy tab in browser setting, once on network, transparent proxies are used.
  2. For HTTP :: Since browser was not aware of any explicit proxy, it addressed the Web socket server and while passing through the transparent proxy, required headers were removed. Websocket is an http upgrade (communicated via certain header, above article have all the details) -since headers were removed, the end server could not make sense of it and treated as simple HTTP
  3. For HTTPS :: Again browser was not aware of proxy, but since data was encrypted –proxy did not tampered the header and request went through to the actual server -since headers were still there all went good and fine!
  4. Spring web socket client (Sockjs) was smart enough to switch to WSS when I used HTTPS –which I did not realize at first shot. Lesson learned -test stuff behind proxy, firewalls even if it is an internal demo 🙂

In actual scenario –in any of corporate deployment, it’s going to be a HTTPS, so seems that web socket have a very good story there!

Spring data -yet another abstraction -quickly getting started


We started with JDBC, then ORM which abstracted the underlying relation DB. Then came JPA -which abstracted the underlying ORM. Now Spring Data

But the kind of versatility we see today in data storage -relational, distributed, nosql and others, to me Spring Data seems to server the purpose.

It abstracts the CRUD for “different” type of data as relational, nosql and just by coding to appropriate interface CRUD operations can be achieved. For getting started use this link for configuring the entity manage, transaction manager -basically the setup stuff.

Write a DaoInterface -here the magic goes, the extended interface decides type of data , relational, nosql etc.

public interface TestDao extends CrudRepository<TestData , Integer> {
}

And then for CRUD operations use this Interface :

@Service
public class TestService{
    @Autowired
    TestDao testDao;
 	public boolean saveTestData(TestData testData){
    	if(testData!=null){
    		testDao.save(user);
                return true;
        }
    	return false;
    }
}
     @Entity
	@Table (name="testtable")
	public class TestData {
	 
	    @Id
	    @GeneratedValue
	    private Long id;
	     
	    public Long getId() {
	        return id;
	    }
	 
	    public void setId(Long id) {
	        this.id = id;
	    }

I found it simple, yet flexible and power full considering the fact that although projects start with SQL, but down the line NOSQL is definitely on their road map -this abstraction will ensure least throwaway code!

Is amazon micro instance of any worth?


I was attracted by per hour pricing of Amazon cloud hosting – to see how things work out, I decided to give it a try.

The Good Part
The entry level is “micro instance” -free of cost for 750 hrs a month. I quickly calculated 24×30=720 -hmm and it works. It says, it good for small traffic -perfect! I just had couple of visitors per HOUR! Nothing can be lesser than that 😉

Background of my APP -its a enterprise app, CPU intensive and high on DB queries. Wanted to demo a couple of clients so was just looking around for some cheap option and I stumbled upon “Free” option.

And The Bad Part
My excitement did not last longer -I noticed a behavior that for a couple of ‘seconds’ things would work as fast as its locally deployed but just a couple of minutes of continuous usage -and thing would come to grinding halt:(

I would have smelled fishy on my side if I could produce this behavior locally, but I have been running this app in LAB environment for a couple of days without any issue.

I spend couple of hours searching for the root cause and I stumble upon this -Thanks god, it saved me a hell lot of effort! Visit this blog and it’s nicely described how “stealed CPU” is the culprit!

So is micro instance any worth ? Only if you want to get a first hand feel of deployments in the cloud and a pat that “hooo -my app is running in ‘cloud’ “, nothing more nothing less!

For starter and less traffic -use their small instance which as on today comes at .080$ in singapore zone. As explained in the blog-at least it will provide consistent behavior.

HAPROXY up and running in couple of minutes


I tried HAPROXY for my WEBAPP (Hosted on tomcat) -reason for using HAPROXY is that it also supports WEBSOCKETS and my current project uses websockets for server push.
As first step, I tested it with bare bone web application -wow, it’s just so easy, without any hiccups it was up and working in ~30 minutes. I used Linux box as no distribution for windows is available (One can use cygwin) -My previous poston same.

  1. Download HAPROXY , I used version haproxy-1.4.24.
  2. Untar it tar -xvf haproxy-1.4.24.tar.gz.
  3. Build HAPROXY, command “make TARGET=linux26” This is for centos58, Linux kernel 2.6. 26 in “TARGET=linux26″ indicates kernel for linux, if its 2.4 use TARGET=linux24”. As a side note, to know the kernel use uname -a on your linux box.
  4. copy haproxy to /usr/sbin use “cp haproxy /usr/sbin/haproxy”
  5. Create a config file say /etc/haproxy_chandan.cfg:
  6. A mentioned on the HAPROXY site, this is the bare minimum configuration needed -Add it to the config file
    global
        daemon
        maxconn 256
    
    defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
    
    frontend http-in
        bind *:80
        default_backend websockets_support
    
    backend websockets_support
        server ws1 a.b.c.d:8888 maxconn 32
        server ws2 a.b.c.d:8080 maxconn 32
    
    listen admin
        bind *:8080
        stats enable
    
  7. Start HAPROXY, /usr/sbin/haproxy -f /etc/haproxy_chandan.cfg
  8. As configured, requests will be handled at port 80 while the admin console for haproxy is 8080

Access your app from http://ipofmachinewherehaproxyisinstalled:80
Access haproxy admin console from http://ipofmachinewherehaproxyisinstalled:8080/haproxy?stats

Thats all. Moving on to configure it for WebSockets and see if it needs additional configuration tweaks or changes -will post.

Load balancer for Web Sockets


I am working on a implementation which requires using websockets with failover to comet. Web socket is an upgrade protocol to HTTP, and once established the connection remains open between server and client. Typical HTPP load balancers will not work – so what is the solution.
I did some research on loadbalancer with specific support for Websockets –it turns out that HAPROX is the most reliable and widely used open source option. Other option is NgNix, having community version as well as paid model –but community version lacks active support.

Apache has also released it’s module for websockets, but reviews are not as good as HAPROXY. A good comparison and discussion around these three is available in this post.

The “Greatest Limitation” of HAPROXY –it does not have a build for windows.
It can be worked out for testing with cygwin on windows -but for any production deployment, it has to be a linux machine.

Never put static files in WEB-INF folder and never be over confident about ur analysis ;)


It could be weird but when you code in hurry to get over ASAP, it invites even more delay and trouble -I spend almost 2 hrs trying to access static files placed in a folder inside WEB-INF folder, forgetting that it will never be accessible from a browser because that’s why there exist a WEB-INF folder -to restrict public access!

So my intent was good – I had a JSP which has embedded SWF file. Everything was in root foler parellal to WEB-INF. Then I though lets restrict access to SWF using direct URL (SWF have some login code as well) – So I created a folder view “inside” WEB-INF and moved all my files there. hmm..

  1. After execution of business logic, I forwarded the request to JSP .
  2. JSP was rendered but SWF was not loaded.
  3. I tried redirect by putting the folders outside WEB-INF (For redirect it has to be out side WEB-INF!) and it worked!!!
  4. Oh my –I concluded, it’s issue with the forward, completely ignoring the fact that it’s about static file location
  5. After 2-3 hrs, yeah, 2-3 hrs, I used Firebug -I do not know why I did not used at the beginning, may be I was trying to be over smart and highly confident about my analysis of the cause!
  6. Firebug reveled that all files need to be at a place accessible by browser -which is outside WEB-INF

More than any technical learning – do not be overconfident about your analysis 😉

Looking up spring beans defined in java configuration classes


Recently I got rid of XML files in Spring app used for configuration by using annotated classes. A basic issue -how to “lookup” these instance if required, took me some time. The good old way with XML

AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/application-context-root.xml");

It’s still simple, use a different class with configuration class as argument

ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class);
sessionManager = (SessionManager) context.getBean("sessionManager");

Never depend on client side dates or any stuff which can be manipulated outside out control


Recently while doing a code review I came across a code which was using date functions in Action script (Adobe client side code) to print payment receipt date.
For persisting the payment date, it was being read from server -But for printing on receipt it was read from client machine.
I got an explanation that why to send a data which can be read same way on client (Referring to equivalent of getCurrentDate) -missing the point that we should never rely of stuff which can be manipulated outside out control.

MySql connections autodrop after a certain hours


MySql is configured to drop any connection which has been Idle for more than 8 hours. What is the implication of this? After you return to your deployed app after a gap of 8 hours (If default SQL parameters have not been changed), you will be greeted with an exception.

How to solve this issue?

  1. Increase the wait_time parameter -Not a good Idea, it might unnecessarily hold on to the resources and not might be a sure shot way. Apart from that, being dependent on an “external” configuration for failover is not a very good idea -what if the server itself crashes, what if this configuration is missed in one of the instnaces, and many such issues will pop up against this approach.
  2. Use the parameter autoReconnect=true with JDBC URL -My SQl itself does not recommend this, have a look at link and people have reported that this does not work as well, refer link.
  3. Custom handling -have your code identify that connection has been lost and then recover it and try to reconnect, but then it would be a lot of fail over mechanism in code.
  4. The best way I found was to configure pooling mechanism as c3p0. See this post how to configure c3p0 in JPA for hibernate, it’s simple, easy and reliable.

So how do you test that issue is solved?

  1. Change wait_timeout in MySql to just 2 minutes, this is how it can be done from MySql workbench admin console mysql_timeout
  2. Keep value of idleTestPeriod less than wait_timeout -A quick recap what idleTestPeriod signifies
  3. idleTestPeriod:  default value=0; If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds
    
  4. Login after wait_timeout has passed -it should not throw a exception

Solving classloading issue while adding pooling support using c3p0 in JPA with hibernate underneath


I added c3p0 for pooling in JPA, but encountered an exception

Unable to load class [org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider]

My configuration looked like

<property name="hibernate.connection.provider_class"
          value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="5" />
<property name="hibernate.c3p0.idle_test_period" value="60" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />	    

Details about these properties and other defined at link

Looking at log trace it’s not difficult to figure out that jar is not correct, so first change, upgrade to latest c3p0 artifact.

Previous

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>3.6.10.Final</version>
</dependency>

Latest

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>4.1.9.Final</version>
</dependency>

After changing this, it worked but printed an Warning

WARN - HHH000208: org.hibernate.connection.C3P0ConnectionProvider has been deprecated in favor of org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider; that provider will be used instead.

This indicates that provider_class should be changed to remove the depricated class, so it should be

<property name="hibernate.connection.provider_class"
          value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />

This cleanly integrated the c3p0 implementation.