init and destroy

69 views
Skip to first unread message

Ray Miller

unread,
Jun 5, 2012, 1:52:12 PM6/5/12
to Ring
Hi,

I'm writing a simple Compojure application and was wondering if it
would benefit from :init and :destroy functions.

The application reads state from the filesystem on startup, and
stashes this in a ref. Currently, this functionality is at the top-
level in core.clj. Would this better be in an init function?

The application also instantiates an executor service, again in the
top-level core.clj. Should this object creation should be done in the
init function? Should I be calling shutdown() in a :destroy function?

Ray.

Robert Marianski

unread,
Jun 5, 2012, 7:58:25 PM6/5/12
to ring-c...@googlegroups.com
I think it depends on where you are going to deploy, and if you need to
aot compile. If you are going to deploy to a container like tomcat, then
I would suggest that you use init/destroy. If you are going to run an
embedded jetty, it might not make a difference.

That being said, I would recommend following the init/destroy pattern
anyway because it's cleaner. Imho it's better not have side effects when
including another file.

Robert

Ray Miller

unread,
Jun 6, 2012, 3:32:31 PM6/6/12
to ring-c...@googlegroups.com
On 6 June 2012 00:58, Robert Marianski <r...@marianski.com> wrote:
>
> I think it depends on where you are going to deploy, and if you need to
> aot compile. If you are going to deploy to a container like tomcat, then
> I would suggest that you use init/destroy. If you are going to run an
> embedded jetty, it might not make a difference.

That raises another question: do people tend to run production
applications with the embedded jetty, or are there good reasons to
deploy to a servlet container? Right now I'm just running 'lein ring
server-headless' from upstart, but I did think about deploying into an
external jetty server.

> That being said, I would recommend following the init/destroy pattern
> anyway because it's cleaner. Imho it's better not have side effects when
> including another file.

Good point. I've moved that functionality into init and destroy
functions. Thank you for your advice.

Ray.

Robert Marianski

unread,
Jun 6, 2012, 4:02:44 PM6/6/12
to ring-c...@googlegroups.com
On Wed, Jun 06, 2012 at 08:32:31PM +0100, Ray Miller wrote:
> On 6 June 2012 00:58, Robert Marianski <r...@marianski.com> wrote:
> >
> > I think it depends on where you are going to deploy, and if you need to
> > aot compile. If you are going to deploy to a container like tomcat, then
> > I would suggest that you use init/destroy. If you are going to run an
> > embedded jetty, it might not make a difference.
>
> That raises another question: do people tend to run production
> applications with the embedded jetty, or are there good reasons to
> deploy to a servlet container? Right now I'm just running 'lein ring
> server-headless' from upstart, but I did think about deploying into an
> external jetty server.

I've used both tomcat and an embedded jetty without issues. If you have
the choice, I think it comes down to what you're more comfortable with
in production. Embedding jetty is an easier deployment story though,
because you don't have the step of building the war file.

Robert

Shantanu Kumar

unread,
Jun 8, 2012, 12:49:45 PM6/8/12
to Ring
Technically, you don't necessarily have to unless you want to deploy
on a servlet container as a webapp. Webapps can be stopped, restarted
etc. when you use Tomcat, Jetty, JBoss etc. as app server. Every time
a webapp is started/stopped that invokes :init and :destroy functions
(`init` and `destroy` in the servlet) respectively, which is a
management feature baked into the servlet spec.

Shantanu

Shantanu Kumar

unread,
Jun 8, 2012, 12:58:05 PM6/8/12
to Ring
> That raises another question: do people tend to run production
> applications with the embedded jetty, or are there good reasons to
> deploy to a servlet container? Right now I'm just running 'lein ring
> server-headless' from upstart, but I did think about deploying into an
> external jetty server.

When you deploy on JBoss, Tomcat etc. you can use management and
monitoring features that are either built into the app server, or
enabled via add-ons e.g. http://code.google.com/p/psi-probe/ . The
general tendency in the Java space is to deploy on an app server in
production to isolate applications, to monitor and to cluster them for
better availability.

Shantanu
Reply all
Reply to author
Forward
0 new messages