Java Concurrency - Web Application

I think I found more errors in my web application. I usually don’t worry about concurrency issues, but when you get a ConcurrentModificationException, you start to rethink your design.

I use JBoss Seam in conjunction with Hibernate and EHCache on Jetty. Now it is one application server with several cores.

I briefly looked through my code and found a few places that have not yet thrown an exception, but I'm sure they can.

The first servlet filter that I have basically checks if there are messages to notify the user of an event that happened in the background (from a job or another user). The filter simply adds messages to the page in a modal popup. Messages are stored in the context of the session, so it is possible that another request may bring the same messages out of the context of the session.

This works fine now, but I don't click on a page with many concurrent requests. I think I might need to write some JMeter tests to prevent this from happening.

The second servlet filter registers all incoming requests along with the session. This allows me to find out where the client is coming from, what browser it is running, etc. The problem that I see recently is the image gallery pages (where there are a lot of requests at about the same time), I get a parallel modification exception because I'm adding a request to the session.

The session contains a list of requests, this list falls into several threads.

@Entity
public class HttpSession 
{
  protected List<HttpRequest> httpRequests;

  @Fetch(FetchMode.SUBSELECT)
  @OneToMany(mappedBy = "httpSession")
  public List<HttpRequest> getHttpRequests()
  {return(httpRequests);}

  ...
}

@Entity
public class HttpRequest
{
  protected HttpSession httpSession;

  @ManyToOne(optional = false)
  @JoinColumn(nullable = false)
  public HttpSession getHttpSession()
  {return(httpSession);}

  ...
}

In this second servlet filter, I am doing something like:

httpSession.getHttpRequests().add(httpRequest);
session.saveOrUpdate(httpSession);

The part that contains errors when I do some comparison to see what has changed from request to request:

for(HttpRequest httpRequest:httpSession.getHttpRequests())

This line explodes with the simultaneous exception of modification.

: 1. JMeter ? 2. -, ? 3. , , , , , , . ?

:

, HTTP- . , . , . , , .

, - JMeter .

concurrency.

,

+3
5

A ConcurrentModificationException , - . , :

for( Object o : someList ) {
  someList.add( new Object() );
}

Collections.synchronizedList .

+5
  1. -, , Java Concurrency in Practice - Concurrency .
  2. , , ( ) . , .
+3

, , . List ConcurrentLinkedQueue ( javadoc) .

:

1: JMeter?

, , , - - concurrency.

2: -, ?

web-, concurrency , Concurrency . -, .

3: , , -, , , , . ?

. ConcurrentLinkedQueue .

0

, , , .

  • ( , ), , , , .

  • - , , , , , .

  • ConcurrentHashMap, . get, , , .

, Java Concurrency - .

0

, , . - . , , , ( ?), , . , . , threadpool , .

concurrency .

0

Source: https://habr.com/ru/post/1731307/


All Articles