You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by John <jo...@quivinco.com> on 2013/11/22 17:03:10 UTC
Tapetsry IoC starting a class with background task that uses an injected service
I have a utility class that recieves emails with a background TimerTask that needs to invoke a method on one of my injected DAOs to store the messages in the db.
How do I get this class to load when I start my app and make sure the injected services are set? With EagerLoad the class gets instantiated but of course the services are null.
John
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Muhammad Gelbana <m....@gmail.com>.
>
> That's my preferred way to do injection in services and it avoids problems
> like you're having
If you do that, you'll have to refactor your constructor parameters and
assignment statement if you remove\add a new service. It should be a lot
easier to use
@Inject
private MyService svc;
If using a constructor makes sure that all passed services (as parameters
to the constructor) are proxied and ready to be instantiated, then so
should injecting a service as a class member (i.e. as the 2 lines of code I
wrote earlier), they should behave the same, else it would be a bug,
exactly like you said.
The only case I know where one MUST use a service's constructor to pass a
dependency, is when you need the logger service injected.
*---------------------*
*Muhammad Gelbana*
http://www.linkedin.com/in/mgelbana
On Fri, Nov 22, 2013 at 7:55 PM, Dmitry Gusev <dm...@gmail.com>wrote:
> @EagerLoad is for service builder methods:
>
> http://tapestry.apache.org/5.3.7/apidocs/org/apache/tapestry5/ioc/annotations/EagerLoad.html
>
> Don't use TimeTask, use Tapestry's PeriodicExecutor with
>
> http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Startup.html
>
>
>
>
> On Fri, Nov 22, 2013 at 9:47 PM, John <jo...@quivinco.com> wrote:
>
> > I need my setup method to run when this class is instantiated but log is
> > null so mailDAO will also be null.
> >
> > John
> >
> >
> > public class EmailReceiverUtil {
> >
> > public static final int SHOW_MESSAGES = 1;
> > public static final int CLEAR_MESSAGES = 2;
> > public static final int SHOW_AND_CLEAR =
> > SHOW_MESSAGES + CLEAR_MESSAGES;
> > /**
> > * The log.
> > */
> > @Inject
> > private Logger log;
> > @Inject
> > private MailDAO mailDAO;
> > /**
> > * Mail properties
> > */
> > Properties props = new Properties();
> > /**
> > * Timer for background task;
> > */
> > Timer timer = new Timer();
> >
> > public EmailReceiverUtil() {
> > System.out.println("EmailReceiverUtil instantiated");
> > }
> >
> > @EagerLoad
> > public void setup() {
> > // configure properties
> > props.put("mail.user", "John");
> > props.put("mail.host", "diskstation");
> > props.put("mail.debug", "false");
> > props.put("mail.store.protocol", "pop3");
> > timer.schedule(new MailReceiverThread(), 1000, 60000);
> > log.info("Email Receiver running");
> > }
> >
> > private void checkInbox(int mode)
> > ...
> > }
> >
> > private boolean processPlainTextMessage(final String from, final
> > String to,
> > final String subject, final String message) {
> > try {
> > String messageText = message;
> > return mailDAO.processIncomingMail(from, to, messageText);
> > } catch (DAOException ex) {
> > ex.printStackTrace();
> > }
> > return false;
> > }
> >
> > class MailReceiverThread extends TimerTask {
> >
> > public void run() {
> > try {
> > checkInbox(CLEAR_MESSAGES);
> > } catch (Exception ex) {
> > ex.printStackTrace();
> > }
> > }
> > }
> > }
> >
> > ----- Original Message -----
> > From: Thiago H de Paula Figueiredo
> > To: Tapestry users
> > Sent: Friday, November 22, 2013 5:26 PM
> > Subject: Re: Tapetsry IoC starting a class with background task that
> > uses an injected service
> >
> >
> > On Fri, 22 Nov 2013 14:03:10 -0200, John <jo...@quivinco.com> wrote:
> >
> > > How do I get this class to load when I start my app and make sure the
> > > injected services are set? With EagerLoad the class gets instantiated
> > > but of course the services are null.
> >
> > This statement is not correct as far as I know. Tapestry-IoC injects
> > service proxies (when the service is defined by an interface), so the
> > dependencies may not even be initialized yet, but, when you first call
> > them, they will be initialized.
> >
> > --
> > Thiago H. de Paula Figueiredo
> > Tapestry, Java and Hibernate consultant and developer
> > http://machina.com.br
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
>
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Dmitry Gusev <dm...@gmail.com>.
@EagerLoad is for service builder methods:
http://tapestry.apache.org/5.3.7/apidocs/org/apache/tapestry5/ioc/annotations/EagerLoad.html
Don't use TimeTask, use Tapestry's PeriodicExecutor with
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Startup.html
On Fri, Nov 22, 2013 at 9:47 PM, John <jo...@quivinco.com> wrote:
> I need my setup method to run when this class is instantiated but log is
> null so mailDAO will also be null.
>
> John
>
>
> public class EmailReceiverUtil {
>
> public static final int SHOW_MESSAGES = 1;
> public static final int CLEAR_MESSAGES = 2;
> public static final int SHOW_AND_CLEAR =
> SHOW_MESSAGES + CLEAR_MESSAGES;
> /**
> * The log.
> */
> @Inject
> private Logger log;
> @Inject
> private MailDAO mailDAO;
> /**
> * Mail properties
> */
> Properties props = new Properties();
> /**
> * Timer for background task;
> */
> Timer timer = new Timer();
>
> public EmailReceiverUtil() {
> System.out.println("EmailReceiverUtil instantiated");
> }
>
> @EagerLoad
> public void setup() {
> // configure properties
> props.put("mail.user", "John");
> props.put("mail.host", "diskstation");
> props.put("mail.debug", "false");
> props.put("mail.store.protocol", "pop3");
> timer.schedule(new MailReceiverThread(), 1000, 60000);
> log.info("Email Receiver running");
> }
>
> private void checkInbox(int mode)
> ...
> }
>
> private boolean processPlainTextMessage(final String from, final
> String to,
> final String subject, final String message) {
> try {
> String messageText = message;
> return mailDAO.processIncomingMail(from, to, messageText);
> } catch (DAOException ex) {
> ex.printStackTrace();
> }
> return false;
> }
>
> class MailReceiverThread extends TimerTask {
>
> public void run() {
> try {
> checkInbox(CLEAR_MESSAGES);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> }
> }
> }
>
> ----- Original Message -----
> From: Thiago H de Paula Figueiredo
> To: Tapestry users
> Sent: Friday, November 22, 2013 5:26 PM
> Subject: Re: Tapetsry IoC starting a class with background task that
> uses an injected service
>
>
> On Fri, 22 Nov 2013 14:03:10 -0200, John <jo...@quivinco.com> wrote:
>
> > How do I get this class to load when I start my app and make sure the
> > injected services are set? With EagerLoad the class gets instantiated
> > but of course the services are null.
>
> This statement is not correct as far as I know. Tapestry-IoC injects
> service proxies (when the service is defined by an interface), so the
> dependencies may not even be initialized yet, but, when you first call
> them, they will be initialized.
>
> --
> Thiago H. de Paula Figueiredo
> Tapestry, Java and Hibernate consultant and developer
> http://machina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
--
Dmitry Gusev
AnjLab Team
http://anjlab.com
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 22 Nov 2013 15:47:10 -0200, John <jo...@quivinco.com> wrote:
> I need my setup method to run when this class is instantiated but log is
> null so mailDAO will also be null.
Weird. Maybe the @EagerLoad-annotated method is being called before the
field injection is done. If that's really the case, that's a bug.
By the way, why don't you receive the dependencies through the
consturctor? That's my preferred way to do injection in services and it
avoids problems like you're having. I don't think I ever used @Inject in a
service.
--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapetsry IoC starting a class with background task that uses an injected service
Posted by John <jo...@quivinco.com>.
I need my setup method to run when this class is instantiated but log is null so mailDAO will also be null.
John
public class EmailReceiverUtil {
public static final int SHOW_MESSAGES = 1;
public static final int CLEAR_MESSAGES = 2;
public static final int SHOW_AND_CLEAR =
SHOW_MESSAGES + CLEAR_MESSAGES;
/**
* The log.
*/
@Inject
private Logger log;
@Inject
private MailDAO mailDAO;
/**
* Mail properties
*/
Properties props = new Properties();
/**
* Timer for background task;
*/
Timer timer = new Timer();
public EmailReceiverUtil() {
System.out.println("EmailReceiverUtil instantiated");
}
@EagerLoad
public void setup() {
// configure properties
props.put("mail.user", "John");
props.put("mail.host", "diskstation");
props.put("mail.debug", "false");
props.put("mail.store.protocol", "pop3");
timer.schedule(new MailReceiverThread(), 1000, 60000);
log.info("Email Receiver running");
}
private void checkInbox(int mode)
...
}
private boolean processPlainTextMessage(final String from, final String to,
final String subject, final String message) {
try {
String messageText = message;
return mailDAO.processIncomingMail(from, to, messageText);
} catch (DAOException ex) {
ex.printStackTrace();
}
return false;
}
class MailReceiverThread extends TimerTask {
public void run() {
try {
checkInbox(CLEAR_MESSAGES);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
----- Original Message -----
From: Thiago H de Paula Figueiredo
To: Tapestry users
Sent: Friday, November 22, 2013 5:26 PM
Subject: Re: Tapetsry IoC starting a class with background task that uses an injected service
On Fri, 22 Nov 2013 14:03:10 -0200, John <jo...@quivinco.com> wrote:
> How do I get this class to load when I start my app and make sure the
> injected services are set? With EagerLoad the class gets instantiated
> but of course the services are null.
This statement is not correct as far as I know. Tapestry-IoC injects
service proxies (when the service is defined by an interface), so the
dependencies may not even be initialized yet, but, when you first call
them, they will be initialized.
--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 22 Nov 2013 14:03:10 -0200, John <jo...@quivinco.com> wrote:
> How do I get this class to load when I start my app and make sure the
> injected services are set? With EagerLoad the class gets instantiated
> but of course the services are null.
This statement is not correct as far as I know. Tapestry-IoC injects
service proxies (when the service is defined by an interface), so the
dependencies may not even be initialized yet, but, when you first call
them, they will be initialized.
--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Dmitry Gusev <dm...@gmail.com>.
Put it to ParallelExecutor?
http://tapestry.apache.org/parallel-execution.html
http://blog.tapestry5.de/index.php/2011/09/18/scheduling-jobs-with-tapestry/
There's also Quartz scheduler support:
https://github.com/anjlab/anjlab-tapestry-commons/tree/master/anjlab-tapestry-quartz
On Fri, Nov 22, 2013 at 8:03 PM, John <jo...@quivinco.com> wrote:
> I have a utility class that recieves emails with a background TimerTask
> that needs to invoke a method on one of my injected DAOs to store the
> messages in the db.
>
> How do I get this class to load when I start my app and make sure the
> injected services are set? With EagerLoad the class gets instantiated but
> of course the services are null.
>
> John
--
Dmitry Gusev
AnjLab Team
http://anjlab.com
Re: Tapetsry IoC starting a class with background task that uses an
injected service
Posted by Muhammad Gelbana <m....@gmail.com>.
I don't believe eager loading loads a service without it's dependencies
injected. Try starting your background service in a @Startup annotation
method in your module's class. Starting your service just means calling one
of it's methods.
*---------------------*
*Muhammad Gelbana*
http://www.linkedin.com/in/mgelbana
On Fri, Nov 22, 2013 at 6:03 PM, John <jo...@quivinco.com> wrote:
> I have a utility class that recieves emails with a background TimerTask
> that needs to invoke a method on one of my injected DAOs to store the
> messages in the db.
>
> How do I get this class to load when I start my app and make sure the
> injected services are set? With EagerLoad the class gets instantiated but
> of course the services are null.
>
> John