Created
November 22, 2011 20:16
-
-
Save marceloverdijk/1386793 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tired of using <code>QueueFactory.getQueue("the-queue").add(TaskOptions.Builder.withPayload(theDeferredTask))</code> in your code, | |
* and want to use something like <code>new MyTask(..).enqueue()</code> or <code>new MyTask(..).enqueue(Queue.THE_QUEUE)</code>? | |
* | |
* See the implementation below with the SendCreateUserConfirmationEmailTask as concrete task to be queued like: | |
* <code>new SendCreateUserConfirmationEmailTask("spam-steve@microsoft.com").enqueue()</code>. | |
*/ | |
// Task.java | |
import com.google.appengine.api.taskqueue.DeferredTask; | |
public interface Task extends DeferredTask { | |
public void enqueue(); | |
public void enqueue(Queue queue); | |
} | |
// Queue.java | |
public enum Queue { | |
DEFAULT("default"), | |
EMAIL("email"); | |
// the email queue is just an exmample, add any queue you want to this enum | |
private final String name; | |
private Queue(String name) { | |
this.name = name; | |
} | |
public String toString() { | |
return name; | |
} | |
} | |
// AbstractTask.java | |
import com.google.appengine.api.taskqueue.QueueFactory; | |
import com.google.appengine.api.taskqueue.TaskOptions; | |
public abstract class AbstractTask implements Task { | |
@Override | |
public void enqueue() { | |
enqueue(Queue.DEFAULT); | |
} | |
@Override | |
public void enqueue(Queue queue) { | |
QueueFactory.getQueue(queue.toString()).add(TaskOptions.Builder.withPayload(this)); | |
} | |
} | |
// SendCreateUserConfirmationEmailTask .java | |
public class SendCreateUserConfirmationEmailTask extends AbstractTask { | |
private String to; | |
public SendCreateUserConfirmationEmailTask(String to) { | |
this.to = to; | |
} | |
@Override | |
public void run() { | |
// send the email, or do whatever your task need to do | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment