# Rails and J2EE middleware (threading/servers)

**URL:** <https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980>\
**Category:** Ruby\
**Created:** [March 15, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980 "2006-03-15T20:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [March 15, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980/1 "2006-03-15T20:56:01Z")

</div>

A lot of applications have a middleware layer. In some ways,  
ActiveRecord or Mailer is thought of as a middleware.

But, for example with J2EE, you can run background servers and threads.  
Can this be done with Ruby and how?

---

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [March 15, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980/2 "2006-03-15T21:09:21Z")

</div>

Berlin B. wrote:

> A lot of applications have a middleware layer. In some ways,  
> ActiveRecord or Mailer is thought of as a middleware.
> 
> But, for example with J2EE, you can run background servers and threads.  
> Can this be done with Ruby and how?

I take the code I need to run separate from rails and set it up as a  
system daemon. I put all of these in my lib/standalone/  
directory. Example:

/var/www/myapp/lib/standalone/report\_generator

/etc/init.d/report\_generator start|stop|restart

My rails app communications via Unix sockets to my report\_generator.

Zach

---

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [March 15, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980/3 "2006-03-15T22:56:24Z")

</div>

That is interesting. I like that approach. How does the ruby client  
code work. Basically, does it timeout or cause a lot of issues.

---

<div class="post-metadata">

**Author:** ![Berlin\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@Berlin\_B](https://www.ruby-forum.com/u/Berlin_B)\
**Post date:** [March 24, 2006, 12:36am UTC](https://www.ruby-forum.com/t/rails-and-j2ee-middleware-threading-servers/54980/4 "2006-03-24T00:36:10Z")

</div>

Berlin B. wrote:

> That is interesting. I like that approach. How does the ruby client  
> code work.

The client just opens a Socket connection to the server, and sends it  
instructions. The server takes the instructions, and the  
server process creates a new thread and does whatever it needs to do  
based on instructions. We make the following assumption:

- the server knows how to parse basic instructions so it can load the  
correct library/worker and pass on worker-specific  
instructions to it
- the client understands the format of instructions

> Basically, does it timeout or cause a lot of issues.

A handful of times it was reported that it didn’t correctly generate the  
report for the end user, however, it was hit half a  
million times in about 2 weeks and it’s hard to prove a handful of times  
something not working. Although I am investigating. =)

The biggest issue we’ve seen is memory consumption. We use this tactic  
for generating CSV reports from a database. Some CSV  
reports which include 45,000 or greater (upward of 275,000) records can  
really suck up the memory. We have avoided this as being a  
huge problem by doing two things:

1. beefing up server memory
2. restarting our worker processes (and daemon processes) periodically  
if memory consumption gets way to high.

There are some changes I am making to my system to fix these initial  
issues, if interested contact me off list…

Zach
