/**
   * Waits for a notification message and checks to see if the message body includes the
   * includedText.
   *
   * @param methodName - the text that should be found in the message body
   */
  private void checkNotification(String pid, String methodName) throws Exception {
    long startTime = System.currentTimeMillis();
    messageNumber++;

    while (true) { // Wait for the notification message
      if (messageCount >= messageNumber) {
        String failureText = "Notification did not include text: " + methodName;
        assertTrue(failureText, currentMessage.getText().contains(methodName));

        failureText =
            "Notification did not include methodName property with " + "value: " + methodName;
        assertTrue(failureText, methodName.equals(currentMessage.getStringProperty("methodName")));

        failureText = "Notification did not include pid property with " + "value: " + pid;
        assertTrue(failureText, pid.equals(currentMessage.getStringProperty("pid")));
        break;
      } else { // Check for timeout
        long currentTime = System.currentTimeMillis();
        if (currentTime > (startTime + messageTimeout)) {
          fail("Timeout reached waiting for notification " + "on message regarding: " + methodName);
          break;
        } else {
          try {
            Thread.sleep(100);
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
      }
    }

    currentMessage = null;
  }
예제 #2
0
  public void testJMSXGroupIdCanBeSet() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "destination:"
            + getQueuePrefix()
            + getQueueName()
            + "\n"
            + "JMSXGroupID: TEST\n\n"
            + "Hello World"
            + Stomp.NULL;

    sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertEquals("Hello World", message.getText());
    // differ from StompConnect
    Assert.assertEquals("TEST", message.getStringProperty("JMSXGroupID"));
  }
예제 #3
0
  public void testSendMessageWithCustomHeadersAndSelector() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue, "foo = 'abc'");

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "foo:abc\n"
            + "bar:123\n"
            + "destination:"
            + getQueuePrefix()
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertEquals("Hello World", message.getText());
    Assert.assertEquals("foo", "abc", message.getStringProperty("foo"));
    Assert.assertEquals("bar", "123", message.getStringProperty("bar"));
  }
예제 #4
0
  @Test(timeout = 60000)
  public void testSendMessageWithStandardHeadersEncoded() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame =
        "CONNECT\n"
            + "login:system\n"
            + "passcode:manager\n"
            + "accept-version:1.1"
            + "\n\n"
            + Stomp.NULL;
    stompConnection.sendFrame(frame);

    frame = stompConnection.receiveFrame();
    assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "correlation-id:c1\\:\\n\\23\n"
            + "priority:3\n"
            + "type:t34:5\n"
            + "JMSXGroupID:abc\n"
            + "foo:a\\bc\n"
            + "bar:123\n"
            + "destination:/queue/"
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    stompConnection.sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(2500);
    assertNotNull(message);
    assertEquals("Hello World", message.getText());
    assertEquals("JMSCorrelationID", "c1\\:\n\\23", message.getJMSCorrelationID());
    assertEquals("getJMSType", "t34:5", message.getJMSType());
    assertEquals("getJMSPriority", 3, message.getJMSPriority());
    assertEquals("foo", "a\\bc", message.getStringProperty("foo"));
    assertEquals("bar", "123", message.getStringProperty("bar"));

    assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID"));
    ActiveMQTextMessage amqMessage = (ActiveMQTextMessage) message;
    assertEquals("GroupID", "abc", amqMessage.getGroupID());
  }
예제 #5
0
  public void testSendMessageWithLongHeaders() throws Exception {
    MessageConsumer consumer = session.createConsumer(queue);

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 1024; i++) {
      buffer.append("a");
    }
    String longHeader = "longHeader:" + buffer.toString() + "\n";

    frame =
        "SEND\n"
            + "correlation-id:c123\n"
            + "persistent:true\n"
            + "priority:3\n"
            + "type:t345\n"
            + "JMSXGroupID:abc\n"
            + "foo:abc\n"
            + longHeader
            + "destination:"
            + getQueuePrefix()
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertEquals("Hello World", message.getText());
    Assert.assertEquals("JMSCorrelationID", "c123", message.getJMSCorrelationID());
    Assert.assertEquals("getJMSType", "t345", message.getJMSType());
    Assert.assertEquals("getJMSPriority", 3, message.getJMSPriority());
    Assert.assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode());
    Assert.assertEquals("foo", "abc", message.getStringProperty("foo"));
    Assert.assertEquals("longHeader", 1024, message.getStringProperty("longHeader").length());

    Assert.assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID"));
  }
예제 #6
0
  public void testSendMessageWithStandardHeaders() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "correlation-id:c123\n"
            + "persistent:true\n"
            + "priority:3\n"
            + "type:t345\n"
            + "JMSXGroupID:abc\n"
            + "foo:abc\n"
            + "bar:123\n"
            + "destination:"
            + getQueuePrefix()
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertEquals("Hello World", message.getText());
    Assert.assertEquals("JMSCorrelationID", "c123", message.getJMSCorrelationID());
    Assert.assertEquals("getJMSType", "t345", message.getJMSType());
    Assert.assertEquals("getJMSPriority", 3, message.getJMSPriority());
    Assert.assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode());
    Assert.assertEquals("foo", "abc", message.getStringProperty("foo"));
    Assert.assertEquals("bar", "123", message.getStringProperty("bar"));

    Assert.assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID"));
    // FIXME do we support it?
    // Assert.assertEquals("GroupID", "abc", amqMessage.getGroupID());
  }
  @Test
  public void shouldSetHeaderOnlyArrayPropertyAnnotation() throws Exception {
    String xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<testMethodHeaderOnly>\n" //
            + "    <arg0>foo</arg0>\n" //
            + "</testMethodHeaderOnly>\n";
    when(message.getText()).thenReturn(xml);
    when(message.getPropertyNames()).thenReturn(new StringTokenizer("arg1[0] arg1[1] arg1[2]"));
    when(message.getStringProperty("arg1[0]")).thenReturn("one");
    when(message.getStringProperty("arg1[1]")).thenReturn("two");
    when(message.getStringProperty("arg1[2]")).thenReturn("three");

    XmlMessageDecoder<TestInterfaceHeaderOnlyArray> decoder =
        XmlMessageDecoder.of(TestInterfaceHeaderOnlyArray.class, implArray);

    decoder.onMessage(message);

    verify(implArray).testMethodHeaderOnly("foo", new String[] {"one", "two", "three"});
  }
  @Test
  public void serviceUpdateMessage_shouldBeReflectedInServicesObject_whenTextMessageReceived()
      throws JMSException {
    Mockito.when(textMessage.getStringProperty("sender")).thenReturn("service");
    Mockito.when(dateTimeProvider.now()).thenReturn(now);
    sut.onMessage(textMessage);

    List<Service> allServices = services.getAllServices();
    assertEquals(1, allServices.size());
    assertEquals("service - 2015-12-03T10:15:30Z", allServices.get(0).getServiceDetails());
  }
  @Test
  public void shouldSetHeaderOnlyStringPropertyAnnotation() throws Exception {
    mockMessage();
    when(message.getStringProperty("arg1")).thenReturn("123");

    XmlMessageDecoder<TestInterfaceHeaderOnlyString> decoder =
        XmlMessageDecoder.of(TestInterfaceHeaderOnlyString.class, implString);

    decoder.onMessage(message);

    verify(implString).testMethodHeaderOnly("foo", "123");
  }
 @Override
 public T take() throws FalconException {
   try {
     TextMessage textMessage = (TextMessage) consumer.receive();
     T event =
         new RerunEventFactory<T>()
             .getRerunEvent(textMessage.getStringProperty("TYPE"), textMessage.getText());
     LOG.debug("Dequeued Message: {}", event.toString());
     return event;
   } catch (Exception e) {
     LOG.error("Error getting the message from ActiveMQ", e);
     throw new FalconException("Error getting the message from ActiveMQ: ", e);
   }
 }
 /**
  * Test that the <code>Message.clearProperties()</code> method deletes all the properties of the
  * Message.
  */
 @Test
 public void testClearProperties_1() {
   try {
     TextMessage message = senderSession.createTextMessage();
     message.setStringProperty("prop", "foo");
     message.clearProperties();
     Assert.assertEquals(
         "sec. 3.5.7 A message's properties are deleted by the clearProperties method.\n",
         null,
         message.getStringProperty("prop"));
   } catch (JMSException e) {
     fail(e);
   }
 }
  /**
   * @param federationExecutionModel
   * @param subscriptionRequestEvent
   * @param message
   */
  public SubscriptionRegistrationFederationExecutionEntry(
      final FederationExecutionModel federationExecutionModel,
      final DataSubscriptionRequest subscriptionRequestEvent,
      final Message message)
      throws MuthurException {
    super();

    // check parameters
    //
    if (federationExecutionModel == null) {
      throw new MuthurException(
          "Error creating JoinFederationExecutionEntry. " + "FederationExecutionModel was null.");
    }

    if (subscriptionRequestEvent == null) {
      throw new MuthurException(
          "Error creating JoinFederationExecutionEntry. "
              + "DataSubscriptionRequestEvent was null.");
    }
    if (message == null) {
      throw new MuthurException(
          "Error creating JoinFederationExecutionEntry. " + "Message was null.");
    }

    try {
      if (message.getJMSReplyTo() == null) {
        throw new MuthurException(
            "Error creating JoinFederationExecutionEntry. " + "Reply queue was null.");
      }

      TextMessage tm = (TextMessage) message;

      dataEventQueueName = tm.getStringProperty(MessageDestination.getDataEventQueuePropName());
    } catch (JMSException e) {
      throw new MuthurException(e);
    }

    if ((dataEventQueueName == null) || ("".equals(dataEventQueueName))) {
      throw new MuthurException(
          "Error creating JoinFederationExecutionEntry. "
              + "Data event queue name was null or empty.");
    }

    /** Set each of the AbstractFederationExecutionEntry attributes */
    setFederationExecutionModel(federationExecutionModel);
    setFederateName(subscriptionRequestEvent.getSourceOfEvent());
    setMessage(message);
    setiEvent(subscriptionRequestEvent);
  }
  @Test
  public void shouldSetHeaderOnlyMapPropertyAnnotation() throws Exception {
    String xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<testMethodHeaderOnly>\n" //
            + "    <arg0>foo</arg0>\n" //
            + "</testMethodHeaderOnly>\n";
    when(message.getText()).thenReturn(xml);
    when(message.getPropertyNames()).thenReturn(new StringTokenizer("arg1[A] arg1[B] arg1[C]"));
    when(message.getStringProperty("arg1[A]")).thenReturn("one");
    when(message.getStringProperty("arg1[B]")).thenReturn("two");
    when(message.getStringProperty("arg1[C]")).thenReturn("three");

    XmlMessageDecoder<TestInterfaceHeaderOnlyMap> decoder =
        XmlMessageDecoder.of(TestInterfaceHeaderOnlyMap.class, implMap);

    decoder.onMessage(message);

    Map<String, String> map = new HashMap<String, String>();
    map.put("A", "one");
    map.put("B", "two");
    map.put("C", "three");
    verify(implMap).testMethodHeaderOnly("foo", map);
  }
예제 #14
0
  /** 处理接收到的消息,通过tag辨别数据类型 */
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
      TextMessage tm = (TextMessage) message;
      String tag = null;
      String text = null;
      try {
        tag = tm.getStringProperty("tag");
        text = tm.getText();

        if (tag != null && text != null) {
          if (tag.equals(WebApiResources.SHUTDOWN_COMMAND)) {
            DataRecorder.getRecorder().shutdownGracefully();
            shutdownGracefully();
          } else {
            DataRecorder.getRecorder().recordMessage(tag, text);
          }
        }
      } catch (JMSException e) {
        e.printStackTrace();
      }
    }
  }
예제 #15
0
  @Test(timeout = 60000)
  public void testSendMessageWithRepeatedEntries() throws Exception {

    MessageConsumer consumer = session.createConsumer(queue);

    String frame =
        "CONNECT\n"
            + "login:system\n"
            + "passcode:manager\n"
            + "accept-version:1.1"
            + "\n\n"
            + Stomp.NULL;
    stompConnection.sendFrame(frame);

    frame = stompConnection.receiveFrame();
    assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SEND\n"
            + "value:newest"
            + "\n"
            + "value:older"
            + "\n"
            + "value:oldest"
            + "\n"
            + "destination:/queue/"
            + getQueueName()
            + "\n\n"
            + "Hello World"
            + Stomp.NULL;

    stompConnection.sendFrame(frame);

    TextMessage message = (TextMessage) consumer.receive(2500);
    assertNotNull(message);
    assertEquals("Hello World", message.getText());
    assertEquals("newest", message.getStringProperty("value"));
  }
예제 #16
0
  private void propertiesPreserved(boolean persistent, boolean messageIDInHeader) throws Exception {
    BridgeImpl bridge = null;

    Connection connSource = null;

    Connection connTarget = null;

    try {
      final int NUM_MESSAGES = 10;

      bridge =
          new BridgeImpl(
              cff0,
              cff1,
              sourceQueueFactory,
              targetQueueFactory,
              null,
              null,
              null,
              null,
              null,
              5000,
              10,
              QualityOfServiceMode.AT_MOST_ONCE,
              1,
              -1,
              null,
              null,
              messageIDInHeader);

      bridge.start();

      connSource = cf0.createConnection();

      connTarget = cf1.createConnection();

      log.trace("Sending " + NUM_MESSAGES + " messages");

      Session sessSource = connSource.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Session sessTarget = connTarget.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageConsumer cons = sessTarget.createConsumer(targetQueue);

      connTarget.start();

      MessageProducer prod = sessSource.createProducer(sourceQueue);

      prod.setDeliveryMode(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);

      TextMessage tm = sessSource.createTextMessage("blahmessage");

      prod.setPriority(7);

      prod.setTimeToLive(1 * 60 * 60 * 1000);

      prod.send(tm);

      long expiration = tm.getJMSExpiration();

      assertEquals(
          persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT,
          tm.getJMSDeliveryMode());

      tm = (TextMessage) cons.receive(1000);

      assertNotNull(tm);

      assertEquals("blahmessage", tm.getText());

      assertEquals(
          persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT,
          tm.getJMSDeliveryMode());

      assertEquals(7, tm.getJMSPriority());

      assertTrue(Math.abs(expiration - tm.getJMSExpiration()) < 100);

      Message m = cons.receive(5000);

      assertNull(m);

      // Now do one with expiration = 0

      tm = sessSource.createTextMessage("blahmessage2");

      prod.setPriority(7);

      prod.setTimeToLive(0);

      prod.send(tm);

      assertEquals(
          persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT,
          tm.getJMSDeliveryMode());

      tm = (TextMessage) cons.receive(1000);

      assertNotNull(tm);

      assertEquals("blahmessage2", tm.getText());

      assertEquals(
          persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT,
          tm.getJMSDeliveryMode());

      assertEquals(7, tm.getJMSPriority());

      assertEquals(0, tm.getJMSExpiration());

      m = cons.receive(5000);

      assertNull(m);

      tm = sessSource.createTextMessage("blahmessage3");

      final boolean myBool = false;
      final byte myByte = (byte) 23;
      final double myDouble = 17625765d;
      final float myFloat = 87127.23f;
      final int myInt = 123;
      final long myLong = 81728712;
      final short myShort = (short) 88;
      final String myString = "ojweodewj";
      final String myJMSX = "aardvark";

      tm.setBooleanProperty("mybool", myBool);
      tm.setByteProperty("mybyte", myByte);
      tm.setDoubleProperty("mydouble", myDouble);
      tm.setFloatProperty("myfloat", myFloat);
      tm.setIntProperty("myint", myInt);
      tm.setLongProperty("mylong", myLong);
      tm.setShortProperty("myshort", myShort);
      tm.setStringProperty("mystring", myString);

      tm.setStringProperty("JMSXMyNaughtyJMSXProperty", myJMSX);

      prod.send(tm);

      tm = (TextMessage) cons.receive(1000);

      assertNotNull(tm);

      assertEquals("blahmessage3", tm.getText());

      assertEquals(myBool, tm.getBooleanProperty("mybool"));
      assertEquals(myByte, tm.getByteProperty("mybyte"));
      assertEquals(myDouble, tm.getDoubleProperty("mydouble"));
      assertEquals(myFloat, tm.getFloatProperty("myfloat"));
      assertEquals(myInt, tm.getIntProperty("myint"));
      assertEquals(myLong, tm.getLongProperty("mylong"));
      assertEquals(myShort, tm.getShortProperty("myshort"));
      assertEquals(myString, tm.getStringProperty("mystring"));
      assertEquals(myJMSX, tm.getStringProperty("JMSXMyNaughtyJMSXProperty"));

      m = cons.receive(5000);

    } finally {
      if (bridge != null) {
        bridge.stop();
      }

      if (connSource != null) {
        connSource.close();
      }

      if (connTarget != null) {
        connTarget.close();
      }
    }
  }
예제 #17
0
  private void messageIDInHeader(boolean on) throws Exception {
    BridgeImpl bridge = null;

    Connection connSource = null;

    Connection connTarget = null;

    try {
      final int NUM_MESSAGES = 10;

      bridge =
          new BridgeImpl(
              cff0,
              cff1,
              sourceQueueFactory,
              targetQueueFactory,
              null,
              null,
              null,
              null,
              null,
              5000,
              10,
              QualityOfServiceMode.AT_MOST_ONCE,
              1,
              -1,
              null,
              null,
              on);

      bridge.start();

      connSource = cf0.createConnection();

      connTarget = cf1.createConnection();

      log.trace("Sending " + NUM_MESSAGES + " messages");

      List ids1 = new ArrayList();

      Session sessSource = connSource.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageProducer prod = sessSource.createProducer(sourceQueue);

      for (int i = 0; i < NUM_MESSAGES; i++) {
        TextMessage tm = sessSource.createTextMessage("message" + i);

        // We add some headers to make sure they get passed through ok
        tm.setStringProperty("wib", "uhuh");
        tm.setBooleanProperty("cheese", true);
        tm.setIntProperty("Sausages", 23);

        // We add some JMSX ones too

        tm.setStringProperty("JMSXGroupID", "mygroup543");
        tm.setIntProperty("JMSXGroupSeq", 777);

        prod.send(tm);

        ids1.add(tm.getJMSMessageID());
      }

      log.trace("Sent the first messages");

      Session sessTarget = connTarget.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageConsumer cons = sessTarget.createConsumer(targetQueue);

      connTarget.start();

      List msgs = new ArrayList();

      for (int i = 0; i < NUM_MESSAGES; i++) {
        TextMessage tm = (TextMessage) cons.receive(5000);

        assertNotNull(tm);

        assertEquals("message" + i, tm.getText());

        assertEquals("uhuh", tm.getStringProperty("wib"));
        assertTrue(tm.getBooleanProperty("cheese"));
        assertEquals(23, tm.getIntProperty("Sausages"));

        assertEquals("mygroup543", tm.getStringProperty("JMSXGroupID"));
        assertEquals(777, tm.getIntProperty("JMSXGroupSeq"));

        if (on) {
          String header = tm.getStringProperty(JBossMessage.JBOSS_MESSAGING_BRIDGE_MESSAGE_ID_LIST);

          assertNotNull(header);

          assertEquals(ids1.get(i), header);

          msgs.add(tm);
        }
      }

      if (on) {
        // Now we send them again back to the source

        Iterator iter = msgs.iterator();

        List ids2 = new ArrayList();

        while (iter.hasNext()) {
          Message msg = (Message) iter.next();

          prod.send(msg);

          ids2.add(msg.getJMSMessageID());
        }

        // And consume them again

        for (int i = 0; i < NUM_MESSAGES; i++) {
          TextMessage tm = (TextMessage) cons.receive(5000);

          assertNotNull(tm);

          assertEquals("message" + i, tm.getText());

          assertEquals("uhuh", tm.getStringProperty("wib"));
          assertTrue(tm.getBooleanProperty("cheese"));
          assertEquals(23, tm.getIntProperty("Sausages"));

          assertEquals("mygroup543", tm.getStringProperty("JMSXGroupID"));
          assertEquals(777, tm.getIntProperty("JMSXGroupSeq"));

          String header = tm.getStringProperty(JBossMessage.JBOSS_MESSAGING_BRIDGE_MESSAGE_ID_LIST);

          assertNotNull(header);

          assertEquals(ids1.get(i) + "," + ids2.get(i), header);
        }
      }

    } finally {
      if (bridge != null) {
        bridge.stop();
      }

      if (connSource != null) {
        connSource.close();
      }

      if (connTarget != null) {
        connTarget.close();
      }
    }
  }
예제 #18
0
  public void testNoMessageIDInHeader() throws Exception {
    BridgeImpl bridge = null;

    Connection connSource = null;

    Connection connTarget = null;

    try {
      final int NUM_MESSAGES = 10;

      bridge =
          new BridgeImpl(
              cff0,
              cff1,
              sourceQueueFactory,
              targetQueueFactory,
              null,
              null,
              null,
              null,
              null,
              5000,
              10,
              QualityOfServiceMode.AT_MOST_ONCE,
              1,
              -1,
              null,
              null,
              false);

      bridge.start();

      connSource = cf0.createConnection();

      connTarget = cf1.createConnection();

      log.trace("Sending " + NUM_MESSAGES + " messages");

      Session sessSource = connSource.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageProducer prod = sessSource.createProducer(sourceQueue);

      for (int i = 0; i < NUM_MESSAGES; i++) {
        TextMessage tm = sessSource.createTextMessage("message" + i);

        // We add some headers to make sure they get passed through ok
        tm.setStringProperty("wib", "uhuh");
        tm.setBooleanProperty("cheese", true);
        tm.setIntProperty("Sausages", 23);

        prod.send(tm);
      }

      log.trace("Sent the first messages");

      Session sessTarget = connTarget.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageConsumer cons = sessTarget.createConsumer(targetQueue);

      connTarget.start();

      for (int i = 0; i < NUM_MESSAGES; i++) {
        TextMessage tm = (TextMessage) cons.receive(5000);

        assertNotNull(tm);

        assertEquals("message" + i, tm.getText());

        assertEquals("uhuh", tm.getStringProperty("wib"));
        assertTrue(tm.getBooleanProperty("cheese"));
        assertEquals(23, tm.getIntProperty("Sausages"));

        String header = tm.getStringProperty(JBossMessage.JBOSS_MESSAGING_BRIDGE_MESSAGE_ID_LIST);

        assertNull(header);
      }
    } finally {
      if (bridge != null) {
        bridge.stop();
      }

      if (connSource != null) {
        connSource.close();
      }

      if (connTarget != null) {
        connTarget.close();
      }
    }
  }
예제 #19
0
  public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
      throw new IllegalArgumentException(
          "JMSBridgeExample needs 2 arguments: <source server> <target server>");
    }
    String sourceServer = args[0];
    String targetServer = args[1];

    System.out.println(
        "client will publish messages to "
            + sourceServer
            + " and receives message from "
            + targetServer);

    // Step 1. Create JNDI contexts for source and target servers
    InitialContext sourceContext = JMSBridgeExample.createContext(sourceServer);
    InitialContext targetContext = JMSBridgeExample.createContext(targetServer);

    Hashtable<String, String> sourceJndiParams = createJndiParams(sourceServer);
    Hashtable<String, String> targetJndiParams = createJndiParams(targetServer);
    // Step 2. Create and start a JMS Bridge
    // Note, the Bridge needs a transaction manager, in this instance we will use the JBoss TM
    JMSBridge jmsBridge =
        new JMSBridgeImpl(
            new JNDIConnectionFactoryFactory(sourceJndiParams, "/source/ConnectionFactory"),
            new JNDIConnectionFactoryFactory(targetJndiParams, "/target/ConnectionFactory"),
            new JNDIDestinationFactory(sourceJndiParams, "/source/topic"),
            new JNDIDestinationFactory(targetJndiParams, "/target/queue"),
            null,
            null,
            null,
            null,
            null,
            5000,
            10,
            QualityOfServiceMode.ONCE_AND_ONLY_ONCE,
            1,
            -1,
            null,
            null,
            true);
    jmsBridge.setTransactionManager(new TransactionManagerImple());

    Connection sourceConnection = null;
    Connection targetConnection = null;
    try {
      jmsBridge.start();
      // Step 3. Lookup the *source* JMS resources
      ConnectionFactory sourceConnectionFactory =
          (ConnectionFactory) sourceContext.lookup("/client/ConnectionFactory");
      Topic sourceTopic = (Topic) sourceContext.lookup("/source/topic");

      // Step 4. Create a connection, a session and a message producer for the *source* topic
      sourceConnection = sourceConnectionFactory.createConnection();
      Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer sourceProducer = sourceSession.createProducer(sourceTopic);

      // Step 5. Create and send a text message to the *source* queue
      TextMessage message =
          sourceSession.createTextMessage(
              "this is a text message sent at " + System.currentTimeMillis());
      sourceProducer.send(message);
      System.out.format(
          "Sent message to %s: %s\n",
          ((Topic) message.getJMSDestination()).getTopicName(), message.getText());
      System.out.format("Message ID : %s\n", message.getJMSMessageID());

      // Step 6. Close the *source* connection
      sourceConnection.close();

      // Step 7. Lookup the *target* JMS resources
      ConnectionFactory targetConnectionFactory =
          (ConnectionFactory) targetContext.lookup("/client/ConnectionFactory");
      Queue targetQueue = (Queue) targetContext.lookup("/target/queue");

      // Step 8. Create a connection, a session and a message consumer for the *target* queue
      targetConnection = targetConnectionFactory.createConnection();
      Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue);

      // Step 9. Start the connection to receive messages from the *target* queue
      targetConnection.start();

      // Step 10. Receive a message from the *target* queue
      TextMessage messageReceived = (TextMessage) targetConsumer.receive(5000);
      System.out.format(
          "\nReceived from %s: %s\n",
          ((Queue) messageReceived.getJMSDestination()).getQueueName(), messageReceived.getText());

      // Step 11. Display the received message's ID and this "bridged" message ID
      System.out.format("Message ID         : %s\n", messageReceived.getJMSMessageID());
      System.out.format(
          "Bridged Message ID : %s\n", messageReceived.getStringProperty("HQ_BRIDGE_MSG_ID_LIST"));
    } finally {
      // Step 12. Be sure to close the resources!
      if (jmsBridge != null) {
        jmsBridge.stop();
      }
      if (sourceContext != null) {
        sourceContext.close();
      }
      if (targetContext != null) {
        targetContext.close();
      }
      if (sourceConnection != null) {
        sourceConnection.close();
      }
      if (targetConnection != null) {
        targetConnection.close();
      }
    }
  }