]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java
Implemented JMS OrderRequest sent by Group.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / cook / Cook.java
1 package at.ac.tuwien.sbc.valesriegler.cook;
2
3 import java.io.Serializable;
4
5 import javax.jms.Connection;
6 import javax.jms.DeliveryMode;
7 import javax.jms.MessageProducer;
8 import javax.jms.ObjectMessage;
9 import javax.jms.Session;
10
11 import org.apache.activemq.ActiveMQConnectionFactory;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
16 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
17
18 public class Cook implements Serializable {
19         private static final Logger log = LoggerFactory.getLogger(Cook.class);
20         private static int nextID = 0;
21         final private int id;
22
23         public static void main(String[] args) throws Exception {
24                 new Cook(++nextID);
25         }
26
27         public Cook(int id) {
28                 this.id = id;
29                 log.info("I AM A COOK WITH ID {}", this.id);
30                 produce(PizzaType.CARDINALE);
31         }
32
33         public int getId() {
34                 return id;
35         }
36
37         public void produce(PizzaType pizzatype) {
38                 try {
39                         // Connecting to the Broker and to the output queue
40                         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
41                         Connection connection = connectionFactory.createConnection();
42                         connection.start();
43                         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
44
45                         MessageProducer producer = session.createProducer(session.createQueue("CookedPizzas"));
46                         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
47
48                         // Create a messages
49                         Pizza pizza = new Pizza(pizzatype, this);
50                         ObjectMessage message = session.createObjectMessage(pizza);
51                         producer.send(message);
52
53                         // Clean up
54                         session.close();
55                         connection.close();
56                 } catch (Exception e) {
57                         log.error("Caught: ", e);
58                         e.printStackTrace();
59                 }
60         }
61 }