예제 #1
0
    public void testRedelivery() throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();

        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        Producer p = s.createProducer(QUEUE, QoS.AT_MOST_ONCE);
        p.send(msg());
        p.close();

        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage m1 = c.receive();
        assertTrue(m1.getHeader().getFirstAcquirer().getValue());
        assertFalse(m1.isSettled());

        s.close();
        s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage m2 = c.receive();
        m2.accept();

        assertTrue(compareMessageData(m1, m2));
        assertFalse(m2.getHeader().getFirstAcquirer().getValue());
        assertNull(get(c));
        conn.close();
    }
예제 #2
0
    private void route(String consumerSource, String producerTarget, String routingKey, boolean succeed) throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();
        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);

        Consumer c = s.createConsumer(consumerSource, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        Producer p = s.createProducer(producerTarget, QoS.AT_LEAST_ONCE);
        AMQPMessage msg = msg();
        AmqpValue sentinel = new AmqpValue(new AMQPDouble(Math.random()));
        msg.setAmqpValue(sentinel);
        Properties props = new Properties();
        props.setSubject(new AMQPString(routingKey));
        msg.setProperties(props);
        p.send(msg);

        if (succeed) {
            AMQPMessage m = c.receive();
            assertNotNull(m);
            assertEquals(sentinel.getValue().getValueString(), m.getAmqpValue().getValue().getValueString());
            m.accept();
        } else {
            assertNull(get(c));
        }
        c.close();
        p.close();
        conn.close();
    }
 public void run() {
   producer.getReady();
   MyBlockingQueue<Integer> buffer = producer.getBuffer();
   Integer item = buffer.take();
   while (item != null) {
     System.out.println(this + " fetches " + item + ".");
     Thread.yield();
     item = buffer.take();
   }
 }
 public static void main(String[] args) throws InterruptedException {
   final int NUM = 10;
   Producer producer = new Producer(10);
   Consumer[] consumers = new Consumer[NUM];
   for (int i = 0; i < NUM; i++) {
     consumers[i] = new Consumer(producer);
   }
   System.out.println("\t== START ==");
   producer.start();
   for (int i = 0; i < NUM; i++) {
     consumers[i].join();
   }
   System.out.println("\t== OVER ==");
 }
예제 #5
0
 /**
  * Create a fat pipe using the threads from the parent
  *
  * @param producer the producer
  * @param pool the group of pipes to add to
  */
 public PooledFatPipe(Producer<T, S> producer, Pool pool, ExceptionListener listener) {
   this.producer = producer;
   this.listener = listener;
   this.pool = pool;
   name = producer.getClass().getSimpleName();
   lock = new Semaphore(0);
 }
예제 #6
0
    public void testRoundTrip() throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();

        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE);
        p.send(msg());
        p.close(); // Settlement happens here
        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage m = c.receive();
        m.accept();
        assertEquals(1, m.getData().size());
        assertEquals(data(), m.getData().get(0));
        conn.close();
    }
예제 #7
0
    public void testReject() throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();

        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE);
        p.send(msg());
        p.close();

        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage m = c.receive();
        m.reject();
        assertNull(get(c));
        conn.close();
    }
 public void setProducer(Producer p) {
   synchronized (this) {
     if (emitting) {
       missedProducer = p != null ? p : NULL_PRODUCER;
       return;
     }
     emitting = true;
   }
   boolean skipFinal = false;
   try {
     currentProducer = p;
     long r = requested;
     if (p != null && r != 0) {
       p.request(r);
     }
     emitLoop();
     skipFinal = true;
   } finally {
     if (!skipFinal) {
       synchronized (this) {
         emitting = false;
       }
     }
   }
 }
예제 #9
0
 private void routeInvalidTarget(String producerTarget) throws Exception {
     AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
     Connection conn = new Connection(ctx, host, port, false);
     conn.connect();
     Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
     try {
         Producer p = s.createProducer(producerTarget, QoS.AT_LEAST_ONCE);
         p.close();
         fail("Target '" + producerTarget + "' should fail");
     }
     catch (Exception e) {
         // no-op
     }
     finally {
         conn.close();
     }
 }
예제 #10
0
    private void decorationTest(DecorationProtocol d, Map<AMQPString, AMQPType> map) throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();
        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE);
        AMQPMessage msg = msg();

        d.decorateMessage(msg, map);
        p.send(msg);
        p.close();
        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage recvMsg = c.receive();
        recvMsg.accept();

        compareMaps(map, d.getDecoration(recvMsg));
        conn.close();
    }
예제 #11
0
 /**
  * Look for data from all the inputs
  *
  * @return true if data was consumed
  */
 private boolean poll(Signal signal, Signal parent) {
   AtomicReferenceArray<T> o = outputs;
   for (int i = 0; i < o.length(); i++) {
     T input = o.getAndSet(i, null);
     if (input == null) continue;
     long seq = ++sequence;
     if (parent != null) parent.signal();
     S product = producer.execute(input, reuseReceptors.get(i), signal);
     signal.signal();
     if (!consume(product, seq, 0)) {
       Thread.yield();
       if (!consume(product, seq, 0))
         logger.info(
             "failed to consume product (" + product + ") from producer (" + producer + ")");
     }
     producer.complete(product);
     return product != null;
   }
   return false;
 }
예제 #12
0
    public void fragmentation(long FrameSize, int PayloadSize)
            throws UnsupportedProtocolVersionException, AMQPException, AuthenticationException, IOException {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.setMaxFrameSize(FrameSize);
        conn.connect();
        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);

        Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE);
        AMQPMessage msg = new AMQPMessage();
        msg.addData(new Data(new byte [PayloadSize]));
        p.send(msg);
        p.close();

        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage m = c.receive();
        m.accept();
        c.close();
        assertEquals(PayloadSize, m.getData().get(0).getValue().length);
        conn.close();
    }
예제 #13
0
  @Override
  public void request(long n) {
    if (n < 0) {
      throw new IllegalArgumentException("n >= 0 required");
    }
    if (n == 0) {
      return;
    }
    synchronized (this) {
      if (emitting) {
        missedRequested += n;
        return;
      }
      emitting = true;
    }
    boolean skipFinal = false;
    try {
      long r = requested;
      long u = r + n;
      if (u < 0) {
        u = Long.MAX_VALUE;
      }
      requested = u;

      Producer p = currentProducer;
      if (p != null) {
        p.request(n);
      }

      emitLoop();
      skipFinal = true;
    } finally {
      if (!skipFinal) {
        synchronized (this) {
          emitting = false;
        }
      }
    }
  }
예제 #14
0
    public void testDataTypes() throws Exception {
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection conn = new Connection(ctx, host, port, false);
        conn.connect();

        Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW);
        Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE);
        AMQPMessage msg = new AMQPMessage();

        List<AMQPType> al = new ArrayList<AMQPType>();
        al.add(new AMQPBoolean(true));
        al.add(new AMQPByte(Byte.MAX_VALUE));
        al.add(new AMQPChar(Character.CURRENCY_SYMBOL));
        al.add(new AMQPDecimal64(BigDecimal.TEN));
        al.add(new AMQPDouble(Double.NaN));
        al.add(new AMQPInt(Integer.MIN_VALUE));
        al.add(new AMQPNull());
        al.add(new AMQPString("\uFFF9"));
        al.add(new AMQPSymbol(new String(new char[256])));
        al.add(new AMQPTimestamp(Long.MAX_VALUE));
        al.add(new AMQPUuid(System.currentTimeMillis(), Long.MIN_VALUE));
        al.add(new AMQPUnsignedShort(0));
        al.add(new AMQPArray(AMQPBoolean.FALSE.getCode(), new AMQPBoolean[]{}));
        al.add(new AmqpSequence(new ArrayList<AMQPType>()));
        AmqpSequence seq = new AmqpSequence(al);
        AmqpValue val = new AmqpValue(seq);
        msg.setAmqpValue(val);

        p.send(msg);
        p.close();
        Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null);
        AMQPMessage recvMsg = c.receive();
        recvMsg.accept();

        assertEquals(val.getValue().getValueString(), recvMsg.getAmqpValue().getValue().getValueString());
        conn.close();
    }
예제 #15
0
 /**
  * Create fat pipe using several threads
  *
  * @param producer the producer
  * @param poolSize the size of the thread group.
  */
 public PooledFatPipe(Producer<T, S> producer, int poolSize, ExceptionListener listener) {
   this(producer, poolSize, listener, producer.getClass().getSimpleName());
 }
예제 #16
0
  void emitLoop() {
    final Subscriber<? super T> c = child;

    outer:
    for (; ; ) {
      long localRequested;
      Producer localProducer;
      Object localTerminal;
      List<T> q;
      synchronized (this) {
        localRequested = missedRequested;
        localProducer = missedProducer;
        localTerminal = missedTerminal;
        q = queue;
        if (localRequested == 0L && localProducer == null && q == null && localTerminal == null) {
          emitting = false;
          return;
        }
        missedRequested = 0L;
        missedProducer = null;
        queue = null;
        missedTerminal = null;
      }
      boolean empty = q == null || q.isEmpty();
      if (localTerminal != null) {
        if (localTerminal != Boolean.TRUE) {
          c.onError((Throwable) localTerminal);
          return;
        } else if (empty) {
          c.onCompleted();
          return;
        }
      }
      long e = 0;
      if (q != null) {
        for (T v : q) {
          if (c.isUnsubscribed()) {
            return;
          } else if (hasError) {
            continue outer; // if an error has been set, shortcut the loop and act on it
          }
          try {
            c.onNext(v);
          } catch (Throwable ex) {
            Exceptions.throwOrReport(ex, c, v);
            return;
          }
        }
        e += q.size();
      }
      long r = requested;
      // if requested is max, we don't do any accounting
      if (r != Long.MAX_VALUE) {
        // if there were missing requested, add it up
        if (localRequested != 0L) {
          long u = r + localRequested;
          if (u < 0) {
            u = Long.MAX_VALUE;
          }
          r = u;
        }
        // if there were emissions and we don't run on max since the last check, subtract
        if (e != 0L && r != Long.MAX_VALUE) {
          long u = r - e;
          if (u < 0) {
            throw new IllegalStateException("More produced than requested");
          }
          r = u;
        }
        requested = r;
      }
      if (localProducer != null) {
        if (localProducer == NULL_PRODUCER) {
          currentProducer = null;
        } else {
          currentProducer = localProducer;
          if (r != 0L) {
            localProducer.request(r);
          }
        }
      } else {
        Producer p = currentProducer;
        if (p != null && localRequested != 0L) {
          p.request(localRequested);
        }
      }
    }
  }