public void startService () { if (!listenerRegistered) { listenerRegistered = true; // Handle messages that could be in the in queue at start time synchronized (sp) { Object[] pending = SpaceUtil.inpAll(sp, in); sp.addListener (in, this); for (Object o : pending) sp.out(in, o); } } } public void stopService () {
/** * @param m message to send * @param timeout amount of time in millis to wait for a response * @return response or null */ public ISOMsg request (ISOMsg m, long timeout) throws ISOException { String key = getKey (m); String req = key + ".req"; sp.out (req, m); sp.out (to, m); ISOMsg resp = (ISOMsg) sp.in (key, timeout); if (resp == null && sp.inp (req) == null) { // possible race condition, retry for a few extra seconds resp = (ISOMsg) sp.in (key, 10000); } return resp; } public void send (ISOMsg m) throws ISOException, IOException {
public Set getKeys () { if (sp instanceof LocalSpace) return ((LocalSpace)sp).getKeySet (); return null; } }
String req = key + ".req"; synchronized (isp) { if (isp.rdp (req) != null) throw new ISOException ("Duplicate key '" + req + "' detected"); isp.out (req, m); sp.out (out, m, timeout); else sp.out (out, m); resp = (ISOMsg) isp.in (key, timeout); if (!shouldIgnore (resp)) break; if (resp == null && isp.inp (req) == null) { resp = (ISOMsg) isp.in (key, 10000);
@Override public boolean process(ISOSource source, ISOMsg m) { sp.out(outQueue, m); return true; }
/** * @param to output queue * @param from input queue * @param unhandled optional unhandled queue (may be null) */ public SpaceMUX (String to, String from, String unhandled) { this (); this.to = to; this.from = from; this.unhandled = unhandled; sp.addListener (from, this); }
public void setConfiguration (Configuration cfg) { if (this.cfg != null) sp.removeListener (from, this); this.cfg = cfg; from = cfg.get ("from"); to = cfg.get ("to"); sp.addListener (from, this); } public void notify (Object key, Object value) {
@Override public void notify(String key, ISOMsg m) { sp.removeListener(this.key, this); ISOSource source = (ISOSource) isp.inp (key); if (m != null && source != null && source.isConnected()) { try { source.send((ISOMsg) m.clone()); sp.inp(key); // actually pick it } catch (Exception e) { Q2.getQ2().getLog().warn(e); } } } }
public boolean isConnected() { if (running() && ready != null && ready.length > 0) { for (String aReady : ready) if (sp.rdp(aReady) != null) return true; return false; } return running(); } public void dump (PrintStream p, String indent) {
public void run (){ long stamp = System.nanoTime(); for (int i=0; i<COUNT; i++) { sp1.in(key); tpsIn.tick(); } long stamp2 = System.nanoTime(); t2.add(stamp2-stamp); System.err.println("Read "+key+" in: "+(stamp2-stamp)/1000000 + " " + tpsIn.toString()); } }
public void close() { if (ssp != null) { ssp.removeListener (key, this); ssp = null; } } }
@Override public int getOutstandingTransactions() { if (iisp instanceof LocalSpace) return ((LocalSpace) iisp).size(queue); return -1; } protected String getKey (String prefix, long id) {
public synchronized void removeListener(Object key, SpaceListener listener) { if (sl != null) sl.inp (new ObjectTemplate (key, listener)); }
private void notifyListeners (Object key, Object value) { Set<SpaceListener> listeners = new HashSet<SpaceListener>(); synchronized (this) { if (sl == null) return; SpaceListener s = null; while ((s = sl.inp(key)) != null) listeners.add(s); for (SpaceListener spl: listeners) sl.out(key, spl); } for (SpaceListener spl: listeners) spl.notify (key, value); }
public void send (ISOMsg m) throws ISOException, IOException { sp.out (to, m); } protected String getFrom () {
public void request (ISOMsg m, long timeout, ISOResponseListener rl, Object handBack) throws ISOException { String key = getKey (m); String req = key + ".req"; synchronized (isp) { if (isp.rdp (req) != null) throw new ISOException ("Duplicate key '" + req + "' detected."); m.setDirection(0); AsyncRequest ar = new AsyncRequest (rl, handBack); synchronized (ar) { if (timeout > 0) ar.setFuture(getScheduledThreadPoolExecutor().schedule(ar, timeout, TimeUnit.MILLISECONDS)); } isp.out (req, ar, timeout); } if (timeout > 0) sp.out (out, m, timeout); else sp.out (out, m); synchronized (this) { tx++; rxPending++; } } public void notify (Object k, Object value) {
public Connector (String from, String to) { this (); this.from = from; this.to = to; sp.addListener (from, this); } public void setConfiguration (Configuration cfg)
public static LocalSpace getSpace (String spaceName) { String key = "jpos:space/"+spaceName; Object obj = getSpace().rdp (key); Space sp = getSpace(); if (obj == null) { synchronized (TransientSpace.class) { obj = sp.rdp (key); if (obj == null) { obj = new TransientSpace (); sp.out (key, obj); } } } return (LocalSpace) obj; } /**