Информациони системи 1/Лаб 1 2021

Извор: SI Wiki
Пређи на навигацију Пређи на претрагу

Grupa B

Posmatra se proces za pravljenje novogodišnjih paketića. Tri radnika prave ratluk, bombone i čokoladice, respektivno. Slatkiši su instance klasa Ratluk, Bombona, Čokoladica. Dakle, napravljeni slatkiš radnik šalje unutar objektne poruke u red za pravljenje koji je zajednički za sve radnike. Radnik kada napravi slatkiš čeka između 3 i 6 sekundi (sa uniformnom raspodelom) i kreće sa pravljenjem novog slatkiša. Sve delove iz reda za pravljenje prikuplja vilenjak koji radi samo sklapanje paketića. Svaki paketić sastoji se iz tačno jednog ratluka, jedne bombone i jedne čokoladice. Kada prikupi sva tri različita slatkiša koja su prva pristigla (nije dozvoljeno da skladišti više slatkiša istog tipa), vilenjak od njih pravi paketić koji je predstavljen klasom Paketić koja u sebi ima reference na svoja tri slatkiša. Vilenjak šalje sve napravljene paketiće na temu. Deda Mraz preuzima paketiće sa te teme. Potrebno je omogućiti da jedan paketić preuzme samo jedan Deda Mraz. Slanje i prijem svake poruke je potrebno ispisati na glavnom izlazu.

Rešenje

Pre pokretanja u Glassfish napraviti Queue sa nazivom myQueue i Topic sa nazivom myTopic, dok takođe treba da postoji podrazumevani Connection Factory jms/__defaultConnectionFactory. Radnici treba da se nalaze u odvojenim projektima kako bi mogli da se pokreću odvojeno, dok ostali paketi mogu da se nalaze u svim projektima.

slatkisi paket

Ovaj paket mora biti u sva tri projekta kako bi procesi uspešno razmenjivali poruke.

Ratluk.java
package slatkisi;

import java.io.Serializable;

public class Ratluk implements Serializable {
    private final int id;
    public Ratluk(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "RATLUK[" + id + "]";
    }
}
Bombona.java
package slatkisi;

import java.io.Serializable;

public class Bombona implements Serializable {
    private final int id;
    public Bombona(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "BOMBONA[" + id + "]";
    }
}
Cokoladica.java
package slatkisi;

import java.io.Serializable;

public class Cokoladica implements Serializable {
    private final int id;
    public Cokoladica(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "COKOLADICA[" + id + "]";
    }
}

paketic paket

Ovaj paket mora da se nalazi i u projektu sa Vilenjakom i u projektu sa Deda Mrazom.

Paketic.java
package paketic;

import slatkisi.*;
import java.io.Serializable;

public class Paketic implements Serializable {
    private final Ratluk ratluk;
    private final Bombona bombona;
    private final Cokoladica cokoladica;
    
    public Paketic(Ratluk ratluk, Bombona bombona, Cokoladica cokoladica) {
        this.ratluk = ratluk;
        this.bombona = bombona;
        this.cokoladica = cokoladica;
    }
    
    @Override
    public String toString() {
        return "PAKETIC[" + ratluk + ", " + bombona + ", " + cokoladica + "]";
    }
}

radnici paket

RadnikRatluk.java

Predstavlja radnike RadnikRatluk, RadnikBombona i RadnikCokoaldica. Pokrenuti tri puta menjajući CANDY_NAME.

package radnici;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.Queue;
import javax.jms.ObjectMessage;
import slatkisi.*;

public class Main {
    @Resource(lookup="jms/__defaultConnectionFactory")
    private static ConnectionFactory connectionFactory;
    
    @Resource(lookup="myQueue")
    private static Queue myQueue;
    
    private static final String CANDY_NAME = "RATLUK";
    private static int staticId = 0;
    
    public static void main(String[] args) {
        JMSContext context = connectionFactory.createContext();
        JMSProducer producer = context.createProducer();
        while (true) {
            try {
                Serializable obj = null;
                switch (CANDY_NAME) {
                    case "RATLUK":
                        obj = new Ratluk(staticId++);
                        break;
                    case "BOMBONA":
                        obj = new Bombona(staticId++);
                        break;
                    case "COKOLADICA":
                        obj = new Cokoladica(staticId++);
                        break;
                }
                ObjectMessage objMsg = context.createObjectMessage(obj);
                objMsg.setStringProperty("Type", CANDY_NAME);
                producer.send(myQueue, objMsg);
                System.out.println("Sent " + obj);
                int wait = (int)((Math.random() * 3 + 3) * 1000);
                Thread.sleep(wait);
            } catch (InterruptedException | JMSException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

vilenjak paket

Vilenjak.java
package vilenjak;

import paketic.Paketic;
import slatkisi.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.ObjectMessage;
import javax.jms.Topic;

public class Main {
    @Resource(lookup="jms/__defaultConnectionFactory")
    private static ConnectionFactory connectionFactory;
    
    @Resource(lookup="myQueue")
    private static Queue myQueue;
    
    @Resource(lookup="myTopic")
    private static Topic myTopic;
    
    public static void main(String[] args) {
        JMSContext context = connectionFactory.createContext();
        JMSConsumer consumerR = context.createConsumer(myQueue, "Type = 'RATLUK'");
        JMSConsumer consumerB = context.createConsumer(myQueue, "Type = 'BOMBONA'");
        JMSConsumer consumerC = context.createConsumer(myQueue, "Type = 'COKOLADICA'");
        JMSProducer producer = context.createProducer();
        while (true) {
            Message msgR = consumerR.receive();
            System.out.println("Received ratluk");
            Message msgB = consumerB.receive();
            System.out.println("Received bombona");
            Message msgC = consumerC.receive();
            System.out.println("Received cokoladica");
            if (msgR instanceof ObjectMessage && msgB instanceof ObjectMessage && msgC instanceof ObjectMessage) {
                try {
                    ObjectMessage objMsgR = (ObjectMessage)msgR;
                    ObjectMessage objMsgB = (ObjectMessage)msgB;
                    ObjectMessage objMsgC = (ObjectMessage)msgC;
                    Ratluk ratluk = (Ratluk)objMsgR.getObject();
                    Bombona bombona = (Bombona)objMsgB.getObject();
                    Cokoladica cokoladica = (Cokoladica)objMsgC.getObject();
                    Paketic paketic = new Paketic(ratluk, bombona, cokoladica);
                    System.out.println("Produced " + paketic);
                    ObjectMessage paekticMsg = context.createObjectMessage(paketic);
                    producer.send(myTopic, paketic);
                    System.out.println("Sent " + paketic);
                } catch (JMSException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

dedamraz paket

DedaMraz.java
package dedamraz;

import paketic.Paketic;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Topic;

public class Main {
    @Resource(lookup="jms/__defaultConnectionFactory")
    private static ConnectionFactory connectionFactory;

    @Resource(lookup="myTopic")
    private static Topic myTopic;

    public static void main(String[] args) {
        JMSContext context = connectionFactory.createContext();
        JMSConsumer consumer = context.createSharedConsumer(myTopic, "sub1");
        while (true) {
            try {
                Message msg = consumer.receive();
                System.out.println("Received paketic");
                if (msg instanceof ObjectMessage) {
                    Paketic paketic = (Paketic)((ObjectMessage) msg).getObject();
                    System.out.println("Received " + paketic);
                }
            } catch (JMSException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }    
}