Java 8 parallelStream with Dropwizard

365 views
Skip to first unread message

Shervin Asgari

unread,
Oct 20, 2014, 2:30:11 PM10/20/14
to dropwiz...@googlegroups.com
Hi.

I am pretty sure I already know the answer to this question, but I want to ask anyways.
Does Dropwizard (Jetty) support parallelStream in Java 8?

I don't think it does since web servers usually doesn't allow thread creation like this.

To do this, in normal Java EE I would annotate a method with @Asynchronous, then the server would do this for me. How can I accomplish simliar behaviour in Dropwizard/Jetty?
I think I need to create "work threads" but I don't know how.
I couldn't find much documentation about this either.

The reason why I need this is because I am planning to send out email, and for obvious reasons I don't want to do this synchronously and block the client.

Thanks in advance!

Ryan Kennedy

unread,
Oct 20, 2014, 6:55:14 PM10/20/14
to dropwiz...@googlegroups.com
I'm not sure I have enough details to answer the question. I haven't played with Java 8 yet, but parallelStream (http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#parallelStream--) and @Asynchronous don't appear to be directly related to one another. Presumably if you're on Java 8 then you should be able to use parallelStream all you want.

And I assume the @Asynchronous you're referring to is from EJB? The newer JAX-RS coming in the dropwizard 0.8.0 upgrade will include more of the async/continuation bits, which may be some of what you're looking for.

You may also want to look at dropwizard's managed executors:


See LifecycleEnvironment#executorService and LifecycleEnvironment#scheduledExecutorService.

Ryan

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shervin Asgari

unread,
Oct 21, 2014, 1:44:13 AM10/21/14
to dropwiz...@googlegroups.com
Hi!

ParallelStream in java 8 uses the fork join framework internally.
All I know is that in normal Java EE enviroment, you won't get the benefit of calling this, since the web server will not allow thread creation like this.

So thats why I wanted to know if this also applies to Dropwizard. 

@Asynchronous is not the same as EJB
In normal Java EE enviroment, if you want to create a new asynchronous thread, you have to call a method that has @Async annotation. For instance, if I want to send out an email without blocking the client.
In a REST application, I think I would probably need to make a new REST call from within my REST method if I wanted to acomplish the same.

LifecycleEnvironment#executorService definetly looks like what I need.
Thanks for the tip. I will investigate further.

Shervin

Shervin Asgari

unread,
Oct 21, 2014, 2:11:18 AM10/21/14
to dropwiz...@googlegroups.com
If LifecycleEnvironment#executorService really works, it means that thread creation is allowed, since ExectorService takes a Callable and Runnable.
AFAIK this means that parallelStream in Java 8 should also work.

Perhaps the reason why this works is because Dropwizard is running Java main, which means you are in a Java SE environment.
I need to investigate some more :)

Shervin

Ryan Kennedy

unread,
Oct 21, 2014, 10:32:27 AM10/21/14
to dropwiz...@googlegroups.com
I've never seen any such thread creation limitation in a dropwizard environment. 

Ryan

Olve Hansen

unread,
Oct 21, 2014, 11:14:51 AM10/21/14
to dropwiz...@googlegroups.com
ParallelStream use Fork Join pool behind the scenes, and should not do i/o or other blocking tasks:

ParallelStream != @Async
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.

Shervin Asgari

unread,
Oct 21, 2014, 12:37:24 PM10/21/14
to dropwiz...@googlegroups.com
I know ParallelStream != @Async
But if you must use @Async, then I know for sure ParallelStream would not work since Java EE container doesn't allow thread creation like the Fork join framework does.
It seems however, this is not an issue with Dropwizard as it runs a java main application. 

Shervin

Olve Hansen

unread,
Oct 21, 2014, 2:03:32 PM10/21/14
to dropwiz...@googlegroups.com
Ok :-) see what you get at now.
Although not recommended, I have never been denied thread creation in J(2)EE environments I have worked in. Maybe the setup was just liberal.

As already concluded you are free to create as many as you wish in DW.

-- 
Olve

Lance N.

unread,
Oct 21, 2014, 3:59:26 PM10/21/14
to dropwiz...@googlegroups.com
It's not a controlled limit. The servlet standard discourages creating side threads. One request, one thread. 
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.

Shervin Asgari

unread,
Oct 21, 2014, 4:11:32 PM10/21/14
to dropwiz...@googlegroups.com

So what does that mean?
Yay or nay on parallell stream?

Shervin

You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/L_TAzOIu_Ag/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.

Shervin Asgari

unread,
Oct 21, 2014, 4:17:00 PM10/21/14
to dropwiz...@googlegroups.com
Its a good chance you havent noticed it before because the app server just treats them as a single thread.
But I am quite sure that you cant create multiple threads from one request like that. In a normal Java EE environment that is.

Shervin

Reply all
Reply to author
Forward
0 new messages