p:commandButton to invoke 2 methods

UI Components for JSF
SebastianD
Posts: 24
Joined: 04 Dec 2010, 14:52
Location: Poland
Contact:

09 Dec 2010, 15:54

I want to invoke 2 methods using one button in one time.
First method is normal action method and it only returns String to process navigation. Second method sends email and it takes some time to finish , but I don't want to wait. I want only to start second method and go to page indicated by first method.
Any ideas how to resolve this problem?

My code looks like this now, it works but I have to wait for second method (sendEmail):

Code: Select all

...
<p:commandButton action="#{myBean.goToPage}" actionListener="#{myBean.sendEmail}" />

...
public String goToPage(){
return "page"
}

public void sendEmail(){
EmailUtils.send();
}
I've tried to mark sendEmail() method by @Asynchronous but it doesn't resolve "waiting" problem.

Franke
Posts: 49
Joined: 12 Oct 2010, 08:41

09 Dec 2010, 16:16

What's wrong with

Code: Select all

<p:commandButton action="#{myBean.goToPage}" />

...
public String goToPage(){
sendEmail();
return "page"
}

public void sendEmail(){
EmailUtils.send();
}
?

Don't make things more complicated then they have to be.
Using PF 3.0 with default JSF implementation of JBoss 6

SebastianD
Posts: 24
Joined: 04 Dec 2010, 14:52
Location: Poland
Contact:

09 Dec 2010, 16:20

The problem is that you have to wait for sendEmail() method to finish goToPage() method. But I don't want to wait.

I want only to start sendEmail() method - because it can take long time to finish.
I want to start and finish goToPage() method ASAP - without waiting for sendEmail().

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

09 Dec 2010, 16:31

You could use a completely different approach.

Create a table in your database to cache all emails you want to send. Then implement some code to send all those cached emails and run the code using a cronjob or something similar.

So you have something like this:

Code: Select all

<p:commandButton action="#{myBean.goToPage}" />

...
public String goToPage(){
cacheEmail();
return "page"
}

public void cacheEmail(){
EmailUtils.cache();
}
//update:

you could user http://jcrontab.sourceforge.net/index.shtml for the cronjobs. You can integrate it within your application server, i never used it myself though.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

09 Dec 2010, 16:35

You can assign another thread to send the email so you dont need to wait, for example springs taskexecutor handles these kind of requirements really well. I've used it when sending emails that take time.

SebastianD
Posts: 24
Joined: 04 Dec 2010, 14:52
Location: Poland
Contact:

09 Dec 2010, 16:44

robert.m, optimus.prime - solutions are ok.

But you know, I can't sleep... I must resolve this problem :shock:
I'm sure that there should be the way to invoke method from jsf (with ajax or not) and don't block my page until it finishes. I don't believe that every call from JSF must block it... :(

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

09 Dec 2010, 16:47

As optimus.prime said, just start a new thread:

Code: Select all

new Thread(new Runnable() {
	@Override
	public void run() {
		SendEmail();
	}
});
Using the JSF-actions there is no other way, because that's not a problem of JSF or PrimeFaces, thats just how Java works. If you want to call a method without wanting to wait for it to finish, you have to start a new thread. There is no way around that.

User avatar
kwintesencja
Posts: 316
Joined: 08 Feb 2010, 20:33
Location: Brazil

09 Dec 2010, 17:24

maybe you can use p:remoteCommand oncomplete of your commandButton.
Att,

--

Rafael Mauricio Pestano


Primefaces 5.x + JavaEE7(Glassfish 4.x and Wildfly 8)
Conventions Framework
Blog
@realpestano

SebastianD
Posts: 24
Joined: 04 Dec 2010, 14:52
Location: Poland
Contact:

10 Dec 2010, 10:47

I will look at p:remoteCommand and it's "async" attribute . Thanks.
But I was thinking a lot about this, and maybe I will do something bigger, that I will use also for other "background" methods, with JMS+Message-Driven Bean.

SebastianD
Posts: 24
Joined: 04 Dec 2010, 14:52
Location: Poland
Contact:

10 Dec 2010, 20:47

kwintesencja - thanks for help. It works just as I wanted :mrgreen:
Later I will publish how to use it - maybe it will help somebody with the same problem.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 29 guests