1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * COPYRIGHT (C) 2001 Reuters.  All Rights Reserved
34   */
35  package com.reuters.msgtest;
36  
37  import com.tibco.tibrv.Tibrv;
38  import com.tibco.tibrv.TibrvDispatcher;
39  import com.tibco.tibrv.TibrvException;
40  import com.tibco.tibrv.TibrvListener;
41  import com.tibco.tibrv.TibrvMsg;
42  import com.tibco.tibrv.TibrvMsgCallback;
43  import com.tibco.tibrv.TibrvQueue;
44  import com.tibco.tibrv.TibrvRvdTransport;
45  import com.tibco.tibrv.TibrvTransport;
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  
50  /***
51   * This mimics a simple server component that enriches the incoming message with
52   * additional data. By default the server listens on the subject MSGTEST.INPUT.
53   * Other subjects can be specified as command line arguments. The two tibrvfields
54   * that are added to the incoming message are ("OrderID",3.14) and
55   * (TradingGroup,"FX"). The message is then sent on the subject MSGTEST.OUTPUT.
56   * The Stimuli and Responses are setup to expect this processing flow.
57   *
58   * @author <a href="mailto:mpollack@MEME"> </a>
59   * @version
60   */
61  public class EnrichmentServer implements TibrvMsgCallback {
62      private Log log = LogFactory.getLog(EnrichmentServer.class);
63      private boolean started;
64      private TibrvDispatcher dispatcher;
65      private String[] subjects;
66  
67      public EnrichmentServer() throws Exception {
68          this(new String[0]);
69      }
70  
71      public EnrichmentServer(String[] subjects) throws Exception {
72          this.subjects = subjects;
73  
74          startup();
75      }
76  
77      /***
78       * @throws TibrvException
79       */
80      public void startup() throws TibrvException {
81          Tibrv.open();
82  
83          TibrvTransport rvTransport = new TibrvRvdTransport();
84          TibrvQueue rvQueue = new TibrvQueue();
85          dispatcher = new TibrvDispatcher(rvQueue);
86  
87          if (this.subjects.length > 0) {
88              for (int i = 0; i < subjects.length; i++) {
89                  new TibrvListener(rvQueue, this, rvTransport, subjects[i], null);
90                  log.debug("Listening on " + subjects[i]);
91              }
92          } else {
93              log.debug("Listening on MSGTEST.INPUT");
94              new TibrvListener(rvQueue, this, rvTransport, "MSGTEST.INPUT", null);
95          }
96  
97          setStarted(true);
98      }
99  
100     public void shutdown() {
101         dispatcher.interrupt();
102         setStarted(false);
103     }
104 
105     /***
106      * Add on the additional tibrvfields ("OrderID",3.14) and (TradingGroup,"FX") and
107      * publish on the subject MSGTEST.OUTPUT
108      *
109      * @param listener
110      *            a <code>TibrvListener</code> value
111      * @param msg
112      *            a <code>TibrvMsg</code> value
113      */
114     public void onMsg(TibrvListener listener, TibrvMsg msg) {
115         try {
116             TibrvMsg reply = new TibrvMsg(msg);
117 
118             if (msg.getReplySubject() != null) {
119                 reply.setSendSubject(msg.getReplySubject());
120             } else {
121                 reply.setSendSubject("MSGTEST.OUTPUT");
122             }
123 
124             reply.add("OrderId", 3.14F);
125             reply.add("TradingGroup", "FX");
126             listener.getTransport().send(reply);
127         } catch (TibrvException e) {
128             log.error("error in enrinchment server", e);
129         }
130     }
131 
132     /***
133      * Run the server.
134      *
135      * @param args
136      *            Subjects that the server should listen on. The default subject
137      *            is MSGTEST.INPUT
138      */
139     public static void main(String[] args) throws Exception {
140         new EnrichmentServer(args);
141     }
142 
143     public boolean isStarted() {
144         return started;
145     }
146 
147     public void setStarted(boolean started) {
148         this.started = started;
149     }
150 } // EnrichmentServer