You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-user@james.apache.org by Gary Jarrel <ga...@gmail.com> on 2007/04/20 10:09:44 UTC

Spring and Mailets

Hi All

Is there any best practices on having Spring inject objects into the mailets?

Thank you!

Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: Spring and Mailets

Posted by Norman Maurer <no...@apache.org>.
Hi Gary,

we don't have any Spring support in james yet :-/

bye
Norman

Gary Jarrel schrieb:
> Hi All
>
> Is there any best practices on having Spring inject objects into the
> mailets?
>
> Thank you!
>
> Gary
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
> For additional commands, e-mail: server-user-help@james.apache.org
>
>
> !DSPAM:1,46287580113111979515896!
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: Spring and Mailets

Posted by Mark Derricutt <ma...@talios.com>.
I'm using spring in one of our mailets to connect up RMI services - works a
treat...

On 4/21/07, Bud Bach <ww...@ameritech.net> wrote:
>
> Gary, I don't know that there are any best practices but I have used
> spring
> in a mailet.  It has worked out well.  If have any specific questions, I
> can
> try to answer.  -- Bud
>
>

RE: Spring and Mailets

Posted by Bud Bach <ww...@ameritech.net>.
Gary, I don't know that there are any best practices but I have used spring
in a mailet.  It has worked out well.  If have any specific questions, I can
try to answer.  -- Bud

> -----Original Message-----
> From: Gary Jarrel [mailto:garyjarrel@gmail.com]
> Sent: Friday, April 20, 2007 3:10 AM
> To: server-user@james.apache.org
> Subject: Spring and Mailets
> 
> Hi All
> 
> Is there any best practices on having Spring inject objects into the
> mailets?
> 
> Thank you!
> 
> Gary
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
> For additional commands, e-mail: server-user-help@james.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: Spring and Mailets

Posted by Gary Jarrel <ga...@gmail.com>.
<snip>
>
> I've not tested it, but it should work if you placed spring stuff in the
> james/SAR-INF/lib folder.
>
> If you try and it works, please confirm it ;-)

I've already got this working, and it actually works quite well.

What I've got happening so far is as follows:

A project has two maven modules:

1) proj-james
2) proj-db

proj-james depends on proj-db. There are a number of spring
configuration files in proj-db and one separate spring configuration
file in proj-james. When maven builds the project it creates a jar for
proj-db, with all the spring configuration files in the root of this
jar.

proj-james is then built with all the dependency jars added into the
root of the jar as well amongst these dependencies is proj-db.jar. I
was hoping that perhaps I can just put this jar under
james_home/apps/james/SAR-INF/lib and everything would work. However
when deployed my mailets were found but as soon as I tried to access
classes out of proj-db.jar I was getting ClassNotFoundException.

The solution was to put all the jars separately under the lib
directory. Hence under the lib directory I have:

- proj-james-nodep.jar (which contains my mailets and the root of the
jar has a spring configuration XML file)
- proj-db.jar (which is all the database logic and a number of other
spring config XML files)
all other dependency jars.

So all the XML files are packaged within the jar files.

I then use an abstract BaseMailet class (which extends GenericMailet)
and in the init method I load all my spring configuration. In this
class I've also put a number of utility methods to get the beans from
Spring. My Mailets then extend the BaseMailet and access the spring
beans using utility method.

All the loading is done using similar code to that described by Bud.

This works, however what I was hoping to achieve was a system where
upon my mailets being instantiated by James, they would automatically
be injected with the required DAOs, etc.

If I wanted to get something like this working, I take it I would need
to modify James. Where should I be looking!

Thank you!

Gary

<snip>

---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: Spring and Mailets

Posted by Stefano Bagnara <ap...@bago.org>.
Bud Bach ha scritto:
> [...]
> The mailet I developed is pretty simple but it does use hibernate to 
> persist the objects.  my applicationContext-service.xml and 
> applicationContext-hibernate.xml files contain my bean definitions 
> and all this gets jar'd up (including the xml files) and I stick 
> it in ${james_home}/lib

In James 2.3.0+ it should be possible to place everything in 
james_home/apps/james/SAR-INF/lib folder and 
james_home/apps/james/SAR-INF/classes is also present in the classpath.

Your xml could probably been simply placed in the classes folder 
(skipping the jar creation).

I've not tested it, but it should work if you placed spring stuff in the 
james/SAR-INF/lib folder.

If you try and it works, please confirm it ;-)

Stefano


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


RE: Spring and Mailets

Posted by Bud Bach <ww...@ameritech.net>.
To elaborate a little further, here is a little of the approach I use.  
I develop in eclipse and I use subversion for SCM. I set up a new eclipse
project and used svn externals to link to common code that shared 
between applications -- MySpringBean for example in the following:

// imports....;

public class MyMailet extends GenericMailet {

  protected MySpringBean mySpringBean;

  // other stuff

  public void init(MailetConfig config) throws MessagingException {
    super.init(config);

    log("------- init begin -------");

    ClassPathXmlApplicationContext appContext = new
      ClassPathXmlApplicationContext(
        new String[] {
          "applicationContext-service.xml", 
          "applicationContext-hibernate.xml"}
      );

    mySpringBean = (MySpringBean) appContext.getBean("mySpringBean");

    log("------- init end -------");

  }

  public void service(Mail mail) throws MessagingException {

    try {
      log("------- servicing new message -------");

      mySpringBean.DoIt(mail);

    } catch (Exception e) {
      log("Error: ", e);
    }
    log("-------------------------------------");

  }

  // other stuff...

}

The mailet I developed is pretty simple but it does use hibernate to 
persist the objects.  my applicationContext-service.xml and 
applicationContext-hibernate.xml files contain my bean definitions 
and all this gets jar'd up (including the xml files) and I stick 
it in ${james_home}/lib

Then in my james config, I add:

 <mailet match="RecipientIs=someone@somewhere.com" class="MyMailet"/>

There may be better ways to go but I hope this helps a little more or helps
someone else facing this.  Sometimes just knowing it can be done is enough!

-- Bud

-----Original Message-----
From: Gary Jarrel [mailto:garyjarrel@gmail.com] 
Sent: Friday, April 20, 2007 3:10 AM
To: server-user@james.apache.org
Subject: Spring and Mailets

Hi All

Is there any best practices on having Spring inject objects into the
mailets?

Thank you!

Gary

---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org