shivang sarawagi

Ranch Hand
+ Follow
since Jun 19, 2008
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by shivang sarawagi

In my application there is a list publisherPostListenerList which receives the real time user posts from the RabbitMQ queue to be sent to the subscribers/consumers. The list is a property of ApplicationListener class which listens to the events of the pubsub queue. The below controller method fetches the list elements via getter method & based on a logic pushes the posts to the subscribers.

The flow is as follows

User writes a post -> Post gets into DB + Queue -> Message from Queue is added in a list which is publisherPostListenerListto be pushed to the subscribers of the user.

As we can see the publisherPostListenerList is a common list for n concurrent requests due to ApplicationListener being a singleton. For a single instance the setup works fine but will fail in a clustered environment as each node will have its own individual publisherPostListenerList list.

How do I deal with this situation? I can't make ApplicationListener class stateless I need the list to store the post elements received from the queue. Do I put the list in a distributed in memory cache? Or there is any other conventional way?

Controller method for pushing the message to the subscriber

7 years ago

I have developed a J2EE app in my local system on a single tomcat, rabbitMQ messaging server & a MYSQL server. Now if I want to scale my app I have to certainly deploy my WAR in a clustered cloud environment.

I went through this resource,,

Because of multiple JVMs involved in a cluster some of the things would definitely fail like concurrent locks, storing global state in a static variable, serialized objects, Singleton etc.

I have two questions

Right from the beginning when writing the first line of code for the app do I keep in mind that the code is to be deployed in a clustered environent & design accordingly using Hazelcast, ZooKeeper etc. to maintain a global state?. Or write regular code using synchronized blocks, statics, singletons etc. Deploy the app on the cluster, test & then make changes accordingly?.
Is there any resource/book which would help on how to write J2EE code to be deployed on a distributed environment?.
7 years ago
Thank you for the response guys I believe handling this scenario at the db level would be the best. Setting up an appropriate database transaction isolation level & lock.
8 years ago
Excuse me if this question may seem naive but I have come across a scenario where I need to manage the product count in the database of an e-commerce store.

There is a Product class with an integer variable productCount which signifies the number of available products in the database which is visible to users of the site. Now this class is accessed by several threads or can say several users of the e-commerce site. Everyone is adding or removing the product to his cart.

The ORM framework being used is hibernate

Sample code

As it is clear from the code that I need to keep a concurrency check on the product count in the database to prevent lost updates.

Also if several users are trying to add only single left product in the database. Which user's cart the product should be added to?

I did a little research on this

Possible ways I found were

Creating a singleton class for Product. That would ensure that just one instance of product is available throughout the application.

Synchronize the & methods. which would allow only one thread to update the product count & update the db at a time.

Use database concurrency control apply some db transaction isolation level, optimistic/pessimistic locking for the productCount. I am using mysql the default isolation level is .

What would be the best approach to deal with this?
8 years ago
I have an AtomicLong field in my class

the counterpart field in the mysql table is BIGINT(20)

as far as the field moviefollowingCount is Long it works great. But as soon as I change it to AtomicLong it throws a SerializationException : could not deserialize while synchronizing the object with the db.

I googled everywhere ideally I should use a long to persist the class property.
Well the class has a field which is the count of people following a movie. The entity is movie. User & Movie have a ManyToMany relationship. Every time a user follows a movie the count gets incremented & updated in the db therefore I've made it AtomicLong as the count is shared amongst multiple users; just like facebook likes for a post.If you think my design is incorrect please advice.

If I make the field as Long how do I share it amongst several threads? Or do I just write a concurrent strategy for the object access & update to & fro from the db & just simply use the long variable?
8 years ago
I have a publish subscribe queue message listener method which fires an event when it reads a message in the queue & adds the message to a publisherPostListenerList in the ApplicationListener class.

The messages added in the list are being pushed to multiple subscribers like real time notifications from the controller method.

After pushing the message to a respective logged in user I am removing the message from the list to avoid the push of redundant message to the user also it keeps in check the size of the list.

The problem is there is only single instance of publisherPostListenerList for all the logged in users. The list being an instance list there should be a separate copy for every logged in user but its not the case. Please correct me if I am wrong.

If I delete a message it deletes the message from the publisherPostListenerList for all the subscribers.

Now my questions are

1. Shouldn't there be an individual copy of publisherPostListenerList list for every logged in user, the list being an instance list.

2. How do I create a separate copy of list for all the logged In users

P.S. I tried changing the bean scope to prototype but didn't work.

Below is the ApplicationEventListener class which holds the list & the controller code.

Controller method for pushing the message to the subscriber

8 years ago
I have implemented a very basic server sent http streaming in my app

Client side code

Server side

The connection is established. But on the UI why does the browser poll for the server every second. The alert is visible on the UI every second.

How does it make different from polling? I saw the same thing happening with the twitter streaming REST API the browser polls the server every second.

8 years ago
I have implemented a very basic server sent http streaming in my app

Client side code

Server side

The connection is established. But on the UI why does the browser poll for the server every second. The alert is visible on the UI every second.

How does it make different from polling? I saw the same thing happening with the twitter streaming REST API the browser polls the server every second.

In my db schema I have two tables

UserPublisher table : It contains info the number of user profiles the currently loggedIn user follows. A single user follows multiple profiles

Post table : It contains posts which the users post.

Now I want to fetch the posts of the user profiles which the current user follows.

I am using hibernate for object relational mapping.

Presently I am fetching the profiles which the user follows & then running the query on POST table for the list of user profiles.

What this be the best approach? I do have to fire two db queries in this scenario, I was hoping could this be achieved may be in one single query or some other way
I am implementing a publish subscribe with Spring AMQP rabbit implementation.

On the consumer end the listener method receives the message from the queue. And then the logic is to send the message to the followers of the user.

Now assume a user has 100 thousand followers. What would be the best approach to handle this?

If the for loop runs a 100 thousand times, considering sending a notification to a follower takes 30 secs it will take a very long time to send a real time notification to all the followers

Or is there a way to iterate through a set of users, split the task into several threads using executor framework/Batch process or something?
8 years ago

I have a user table & a child table Post containing user's posts. A user has a large number of posts just like twitter posts.

I need to fetch user posts on the UI, load more posts as the user scrolls down the page just like facebook/twitter do. I am using hibernate as the ORM framework & MySql as db. I looked into pagination found two primary ways of achieving that

I have two questions

A. Which way of pagination would be more appropriate & efficient to achieve this? I've read that ScrollableResults is more efficient than setFirstResult but it keeps the connection open for the entire pagination process.

B. As the user's profile page is loaded ajax call is fired to display the user's posts everytime, it's like a certain default content in the page. So do I need to implement a second level cache in order to avoid db hits every time the page loads?
I am confused about a scenario

I need to navigate/render a jsp when a custom exception is thrown from a code block in my app.

for that in web.xml I have configured the error handling

Code block

My questions are:

Is this the correct way to invoke a jsp when my code throws an exception?
Do I need a try catch block to handle the thrown RecordNotFoundException, if yes how would I navigate to the jsp from the catch block?
9 years ago
Fixed it.


From dispatcher-servlet.xml to application-context.xml

Started working fine.
10 years ago
I am implementing Spring Security in project. I have reached an impasse stuck here since hours.

I am getting this error

The project setup is very simple




I also tried declaring the bean in both dispatcher-servlet.xml & application-context.xml it doesn't work

checked the context-component base package. It is scanning all the other classes present just fine. When I remove myUserDetailService from authentication provider the server starts just fine without any error.

I am really tired. Can anyone please help me in fixing this?
10 years ago
Thanks for the reply guys. Can you please add any resource link or would want to elaborate on session saving strategies?.
10 years ago