Changeset 72 for trunk/src/test/java/omq


Ignore:
Timestamp:
06/29/13 20:44:27 (11 years ago)
Author:
stoda
Message:

Events deleted instead of them there's a new example of how to use the observer pattern

Location:
trunk/src/test/java/omq/test
Files:
2 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/test/java/omq/test/calculator/Calculator.java

    r47 r72  
    11package omq.test.calculator;
    2 
    3 import java.io.IOException;
    42
    53import omq.Remote;
     
    75import omq.client.annotation.RemoteInterface;
    86import omq.client.annotation.SyncMethod;
    9 import omq.exception.SerializerException;
    107
    118@RemoteInterface
     
    2017        public void sendMessage(Message m);
    2118
    22         @AsyncMethod
    23         public void asyncDivideByZero() throws IOException, SerializerException;
    24 
    2519        @SyncMethod(timeout = 1500)
    2620        public int divideByZero();
  • trunk/src/test/java/omq/test/calculator/CalculatorImpl.java

    r63 r72  
    11package omq.test.calculator;
    22
    3 import java.io.IOException;
    4 
    5 import omq.common.broker.Broker;
    6 import omq.exception.SerializerException;
    73import omq.server.RemoteObject;
    84
    95public class CalculatorImpl extends RemoteObject implements Calculator {
    106        private int mult = 0;
    11         private Broker broker;
    127
    138        public CalculatorImpl() throws Exception {
    149                super();
    15         }
    16 
    17         public CalculatorImpl(Broker broker) throws Exception {
    18                 super();
    19                 this.broker = broker;
    2010        }
    2111
     
    4131
    4232        @Override
    43         public void asyncDivideByZero() throws IOException, SerializerException {
    44                 ZeroEvent ze = new ZeroEvent("my zero event", "zero-event");
    45                 broker.trigger(ze);
    46                 // notifyEvent(ze);
    47         }
    48 
    49         @Override
    5033        public void sendMessage(Message m) {
    5134                System.out.println("Code = " + m.getCode());
  • trunk/src/test/java/omq/test/calculator/CalculatorTest.java

    r62 r72  
    120120
    121121        @Test
    122         public void notifyEvent() throws Exception {
    123                 ZeroListener zL = new ZeroListener("zero-event");
    124 
    125                 remoteCalc.addListener(zL);
    126 
    127                 remoteCalc.asyncDivideByZero();
    128 
    129                 Thread.sleep(200);
    130         }
    131 
    132         @Test
    133122        public void sendMessage() throws Exception {
    134123                Message m = new Message(2334, "Hello objectmq");
  • trunk/src/test/java/omq/test/observer/ObserverTest.java

    r71 r72  
    2323        private static String OBSERVER = "observer";
    2424        private static Broker broker;
     25        private static RemoteSubjectImpl subject;
    2526        private static RemoteSubject remoteSubject;
    2627
     
    7172                env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000");
    7273
     74                // Set info about the queue & the exchange where the ResponseListener
     75                // will listen to.
     76                env.setProperty(ParameterQueue.RPC_REPLY_QUEUE, "server_reply_queue");
     77                env.setProperty(ParameterQueue.EVENT_REPLY_QUEUE, "server_event_queue");
     78
    7379                Broker broker = new Broker(env);
    74                 RemoteSubjectImpl subject = new RemoteSubjectImpl();
     80                subject = new RemoteSubjectImpl(broker);
    7581                broker.bind(SUBJECT, subject);
    7682
     
    9096                RemoteObserverImpl observer = new RemoteObserverImpl();
    9197                broker.bind(OBSERVER, observer);
     98                observer.setSubject(remoteSubject);
    9299
    93                 remoteSubject.addObserver(observer);
     100                remoteSubject.addObserver(observer.getRef());
    94101                remoteSubject.setState(expected);
    95                 remoteSubject.notifyObservers();
     102                // both proxies are in the same thread for this reason since subject has
     103                // a proxy inside we need to use subject or create a new thread
     104                subject.notifyObservers();
    96105                actual = observer.getObsState();
    97106
    98107                assertEquals(expected, actual);
    99                 remoteSubject.setState(null);
     108                remoteSubject.setState("");
    100109        }
    101110
  • trunk/src/test/java/omq/test/observer/RemoteSubject.java

    r71 r72  
    44import omq.client.annotation.RemoteInterface;
    55import omq.client.annotation.SyncMethod;
     6import omq.exception.RemoteException;
    67
    78@RemoteInterface
     
    1516
    1617        @SyncMethod(timeout = 1000)
    17         public void addObserver(RemoteObserver o);
     18        public void addObserver(String ref) throws RemoteException;
    1819
    1920        @SyncMethod(timeout = 1000)
    20         public void removeObserver(RemoteObserver o);
     21        public void removeObserver(String ref) throws RemoteException;
    2122
    2223        @SyncMethod(timeout = 1000)
  • trunk/src/test/java/omq/test/observer/RemoteSubjectImpl.java

    r71 r72  
    44import java.util.List;
    55
     6import omq.common.broker.Broker;
     7import omq.exception.RemoteException;
    68import omq.server.RemoteObject;
    79
     
    1315        private static final long serialVersionUID = 1L;
    1416        private String state;
    15         private List<RemoteObserver> list = new ArrayList<RemoteObserver>();
     17        private Broker broker;
     18        private List<RemoteObserver> list;
    1619
    17         @Override
    18         public void addObserver(RemoteObserver o) {
    19                 list.add(o);
     20        public RemoteSubjectImpl(Broker broker) {
     21                this.broker = broker;
     22                list = new ArrayList<RemoteObserver>();
    2023        }
    2124
    2225        @Override
    23         public void removeObserver(RemoteObserver o) {
    24                 list.remove(o);
     26        public void addObserver(String ref) throws RemoteException {
     27                RemoteObserver obs = broker.lookup(ref, RemoteObserver.class);
     28                list.add(obs);
     29        }
     30
     31        @Override
     32        public void removeObserver(String ref) throws RemoteException {
     33                RemoteObserver obs = broker.lookup(ref, RemoteObserver.class);
     34                list.remove(obs);
    2535        }
    2636
Note: See TracChangeset for help on using the changeset viewer.