-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.java
64 lines (54 loc) · 2.02 KB
/
Worker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Main;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.naming.NamingException;
public abstract class Worker implements MessageListener {
private JMSHelper jmsHelper;
private MessageConsumer workerQueueReader;
private MessageProducer resultQueueWriter;
public Worker() throws NamingException, JMSException {
jmsHelper = new JMSHelper();
workerQueueReader = jmsHelper.createWorkerQueueReader();
workerQueueReader.setMessageListener(this);
resultQueueWriter = jmsHelper.createResultQueueSender();
System.out.println("All set up, starting Worker!");
this.start();
}
public Worker(MessageConsumer consumer, MessageProducer producer) throws NamingException, JMSException {
workerQueueReader = consumer;
resultQueueWriter = producer;
}
public void onMessage(Message jmsMessage) {
try {
Job newJob = (Job)((ObjectMessage) jmsMessage).getObject();
System.out.println("New Job: "+ newJob);
Object result = handleResult(handleNewJob(newJob));
if(result!=null){
newJob.setResponse(result);
this.addToResultQueue(newJob);
}
} catch (JMSException e) {
System.err.println("Failed to receive message");
}
}
public void start(){
while(true) {}
}
//TODO
private void addToResultQueue(Object result) throws JMSException{
this.resultQueueWriter.send(prepare(result));
}
public ObjectMessage prepare(Object result) throws JMSException {
if(jmsHelper != null)
return this.jmsHelper.createMessage((Serializable) result);
else
throw new JMSException("Unable to create ObjectMessage from Serializable object; Fix by adding a JMSHelper or forcefully use workerQueueWriter.send(job)");
}
public abstract Object handleNewJob(Job newJob);
public abstract Object handleResult(Object result);
}