Prabhu Sunderaraman

Programmer, Speaker, Writer, Trainer

prabhu.bits@gmail.com,     GitHub     Twitter     Facebook     Youtube     LinkedIn
  • Home
  • Profile
  • Books
  • Reading List
Browsing: / Home
Shortlink

switch-case is more expressive in Java 12 & above

By Prabhu Sunderaraman on September 25, 2019 in Java, Java 8

One of the features that I am really excited about is pattern matching in Java. Pattern matching is a powerful feature in languages like Scala and Kotlin. You have an input token that’s matched against patterns.
switch-case statement comes close, yet very far, to pattern matching in Java. But starting Java 12 you’ve switch-case available as expressions and not statements, that enables us harness the power of pattern matching.

Here’s an example of using switch-case statement before Java 12.

Before

public class PatternMatching  {
	public static void main(String[] args) {
		int number = 12;
		int mod = number % 2;
		String result = null;
		switch(mod) {
			case 1:
				result = "Odd";
				break;
			case 0:
				result = "Even";
				break;
			default:
				result = "Huh";
		};
		System.out.println(result);
	}
}

And an example that uses switch-case expressions in Java 12.

After

public class PatternMatching  {
	public static void main(String[] args) {
		int number = 12;
		int mod = number % 2;
		
		String result = switch(mod) {
			case 0 -> "Even";
			case 1 -> "Odd";
			default -> "Huh";
		};
		System.out.println(result);
	}
}

You can compile& run PatternMatching.java by enabling preview mode like this.
javac --enable-preview --release 12 PatternMatching.java
java --enable-preview PatternMatching

You can watch the video here.




Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Project with 18K lines of code in a class (Challenges) – Part II

By Prabhu Sunderaraman on July 21, 2019 in Cloud, Docker, JavaScript, Software Architecture, Spring

In the previous post, we discussed about the nature of the application highlighting lack of some key items. Let’s discuss the deployment infrastructure of the application.
The application is deployed in a jboss domain cluster. There are three nodes in the cluster with a load balancer in the front. Each JBoss server has an Apache Web server that serve’s as a Reverse Proxy. There are a couple of other standalone jboss servers that are used for tasks to run jobs, perform some tasks like document conversions etc., A couple of identity servers used for authentication and authorization implemented using Shibboleth.
The application is file IO intensive. So there’s a SAN based storage volume created just for storing files and the volume is mounted on each server. And there’s ElasticSearch instance and MySql instance in the background.

Screen Shot 2019-07-21 at 7.52.41 PM

So far so good!!! Before we jump into the rationale behind using Jboss, domain clusters etc, let’s talk about the challenges.

Running out of Memory

The first and foremost problem is servers running out of memory. Each server is 16 core processor 32GB RAM, bare-metal machine and not a VM. The JVM memory utilization hits 90% every 2 days and stops responding. So there’s a manual restart required. The servers are hosted in a localized infrastructure provider. For a specific set of customers we had the infrastructure in AWS as well.

Cluster crash

When one of the instances is down, the other two instances keep handling the requests. But when you try to bring up the instance that went down, the other two instances also crash. So everytime we’re forced to restart the whole cluster, which eats up sweet 15 minutes as there is no automated way to do this as I mentioned in the earlier post.

Running out of Space in SAN volume

The SAN storage area eats up so much data to the tune of approximately 1 TB per month. So maintaining it, backing it up, cleaning it up is a huge mess. Often it goes out of space and the application becomes unresponsive. Increasing the size or reclaiming is a tedious task.

The Load balancer issue

Ideal job of load balancers is to balance out the load on the servers. It scans all the incoming requests and directs them to the appropriate servers that are capable of handling the requests at any point of time.

This application had a specific problem based on a particular file upload functionality. The front end implemented a file upload functionality using jQuery uploader plugin. When the size of the file is huge, the implementation automatically splits into chunks and uploads the file. So roughly a 10 MB file gets uploaded as 7-8 chunks to the server. In the server, after the chunks are uploaded, a background job merges all the chunks and pushes them to the storage area.

So this is how it works in a non-load-balancer environment.

Screen Shot 2019-07-21 at 7.38.14 PM

Let’s wear our thinking hat on for sometime before we delve into the challenges of this particular functionality in a load-balanced environment in our next post.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Project with 18K lines of code in a class – Part I

By Prabhu Sunderaraman on July 17, 2019 in Cloud, Java 8, JavaScript, Micro services, Spring

I have been consulting for a company say X, the last few months. X is one of those companies that has a product used by several clients, with approximately 100K transactions per day, but with code that has become unmaintainable, very hard to make changes. I got a call from them, asking for my services in improving the performance, migrating to the cloud, moving their application to microservices architecture and building a team.
Having been there, done that, I always get into these consulting assignments with not a pinch of salt but bagful of salt.

blog#1

The application has been built by a number of people in the past, passed on to so many others and now being maintained and worked upon by few hapless developers. It’s a familiar story in software development. Isn’t it?

Experience has always taught me to be a silent observer during the start of these assignments. So I understood in a no time about the nature of the application.

  • It didn’t have a proper version control mechanism. SVN was being used as recycle bin. So no Version Control strategy
  • No MAVEN/GRADLE. Yes you read it right!!! Every jar file was manually downloaded and copied in the project. I didn’t want to get into the Whys? of this
  • ZERO test cases. I don’t get surprised at all these days when I come across applications with no test cases. I have long before come to accept that it requires a certain amount of skill and maturity from developers and managers to understand the importance of having test cases.
  • No automated builds/deployments. Every two weeks application was deployed manually in all the servers in QA and production environment
  • Monolith application, with 400MB of WAR file built and deployed
  • Spring application with framework concepts of Spring being used/abused extensively.

Now getting inside the guts of the application, here’s what I found.

  • There are approximately 700 JSPs.
  • Each JSP has 500 lines of code on an average. It’s a spaghetti of jQuery, JSP scriptlets, CSS and HTML code very hard to read
  • Tens of thousands of classes
  • The best is here. Each class has on an average 5000 lines of code. The longest class had 18000 lines of code. And there were easily a hundreds of such classes. During my course of stay, when I set up Git and tried to open one such class, GitHub politely refused to open saying “It’s too long to open”
  • Code written in Java 8, but not even a single class has any feature related to Java 8 used.
  • Oh, duplication of code, thy name is X

It takes a gargantuan effort to streamline these kind of applications. One of the things, I have learnt over several years of coding and personal startup experience is maintaining the quality of an application is a never ending and continuous task. One needs to be constantly conscious about the way the application evolves and have a strong pulse on the quality.

The pressure to meet the deadlines are too much for a developer to think about the quality. After sometime, your focus shifts to just delivering what’s required and quality takes a backseat. The bitter truth is it comes back to haunt you after sometime. That’s why the wisest have said, Karma is a bitch :)

Inspite of these shortcomings, the deployment infrastructure was the saviour which held this whole application together. But it was a double-edged sword. Let’s talk about this in the next post. In the subsequent posts, I will talk about how we went about solving some of the issues.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

ReactJS video series – State with React – (Part 2)

By Prabhu Sunderaraman on February 8, 2019 in JavaScript, React JS

This is the eighth part of the video series on React JS.

We continue our discussions on the basics of state in React JS. State represents the data model of a component. It’s bound to the UI. Technically, it’s just JSON data. Changing the state data using setState() method invokes the render() method. We have a button in this example, clicking which modifies the state variable, resulting in UI update

The example used in the video is given below

<html>
    <head>
        <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
        <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
        <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    </head>
            
    <body>
        <div id="root"></div>
        <script type="text/jsx">
          class Sample extends React.Component {
              constructor(props) {
                  super(props);
                  this.state = {
                      currentTime: new Date()
                  };
              }

              getCurrentTime() {
                  let currentTime = new Date();
                  //setState method is used to update any item in the state
                  this.setState({
                      currentTime
                  })
              }

              render() {
                  return (<div>
                      <h1>Time now: {this.state.currentTime.toString()}</h1>
                      <button onClick={this.getCurrentTime.bind(this)}>Get Current Time</button>
                  </div>);
              }
          }
          ReactDOM.render(<Sample/>, document.getElementById("root"));  
        </script>
    </body>        
</html>

You can find the video here.

Read and watch the other parts here.

1. Hello ReactJS without JSX
2. Hello ReactJS with JSX
3. Let’s play with JSX – Part I
4. Let’s play with JSX – Part II
5. Creating Components – Part I
6. Creating Components – Part II
7. State with React – Part I

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

ReactJS video series – State with React – (Part 1)

By Prabhu Sunderaraman on February 7, 2019 in JavaScript, React JS

This is the seventh part of the video series on React JS.

We discuss the basics of state in React JS. State represents the data model of a component. It’s bound to the UI. Technically, it’s just JSON data.

The example used in the video is given below

<html>
    <head>
        <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
        <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
        <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    </head>
            
    <body>
        <div id="root"></div>
        <script type="text/jsx">
          class Sample extends React.Component {
              constructor(props) {
                  super(props);
                  this.state = {
                      currentTime: new Date()
                  };
              }

              render() {
                  return (<div>
                      <h1>Time now: {this.state.currentTime.toString()}</h1>
                  </div>);
              }
          }
          ReactDOM.render(<Sample/>, document.getElementById("root"));  
        </script>
    </body>        
</html>

You can find the video here.

Read and watch the other parts here.

1. Hello ReactJS without JSX
2. Hello ReactJS with JSX
3. Let’s play with JSX – Part I
4. Let’s play with JSX – Part II
5. Creating Components – Part I
6. Creating Components – Part II

Share this on: Mixx Delicious Digg Facebook Twitter
1 2 … 54 Next »

Youtube Channel




Categories

  • JavaScript (48)
    • RequireJS (5)
  • Ext JS (23)
  • Spring (22)
  • Mobile (21)
  • Scala (20)
    • Play (3)
  • Uncategorized (19)
  • Video Sessions (18)
  • GoG (17)
  • Sencha Touch (16)
  • jQuery (14)
  • Languages (13)
  • React JS (11)
  • Kotlin (11)
  • HealthyCodeMagazine (9)
  • Java 8 (9)
  • Video (9)
  • Objective-C (8)
  • NoSQL (8)
  • Android (7)
  • MongoDB (7)
  • GWT (6)
  • Tools (6)
  • HTML 5 (5)
  • Cloud (5)
  • General (5)
  • Micro services (5)
  • Books (4)
  • AWS (4)
  • Software Architecture (4)
  • .NET (3)
  • Elixir (3)
  • Docker (3)
  • Reactive (3)
  • NodeJS (2)
  • RoR (2)
  • Backbone (1)
  • AngularJS (1)
  • Java (1)

Archives

  • 2019 (7)
  • 2018 (34)
  • 2017 (15)
  • 2016 (7)
  • 2015 (18)
  • 2014 (31)
  • 2013 (55)
  • 2012 (100)

Search

Subscribe




Copyright © 2019 Prabhu Sunderaraman.

Powered by WordPress and News.