当前位置: 首页>>代码示例>>Java>>正文


Java ActiveMQConnectionFactory.createConnection方法代码示例

本文整理汇总了Java中org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection方法的典型用法代码示例。如果您正苦于以下问题:Java ActiveMQConnectionFactory.createConnection方法的具体用法?Java ActiveMQConnectionFactory.createConnection怎么用?Java ActiveMQConnectionFactory.createConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory的用法示例。


在下文中一共展示了ActiveMQConnectionFactory.createConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDefaultConstructor

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testDefaultConstructor() throws Exception {
   ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF);
   assertFactoryParams(cf, null, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
   Connection conn = null;

   try {
      conn = cf.createConnection();

      conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Assert.fail("Should throw exception");
   } catch (JMSException e) {
      // Ok
   }
   if (conn != null) {
      conn.close();
   }

   ActiveMQConnectionFactoryTest.log.info("Got here");

   testSettersThrowException(cf);
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:24,代码来源:ActiveMQConnectionFactoryTest.java

示例2: testTwoConnectionsSameIDThroughCF

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testTwoConnectionsSameIDThroughCF() throws Exception {
   ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616?clientID=myid");

   conn = connectionFactory.createConnection();
   try {
      conn2 = connectionFactory.createConnection();
      Assert.fail("Exception expected");
   } catch (InvalidClientIDException expected) {
      // expected
   }


   Session session1 = conn.createSession();
   Session session2 = conn.createSession();

   session1.close();
   session2.close();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:20,代码来源:ConnectionTest.java

示例3: testDiscoveryConstructor

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testDiscoveryConstructor() throws Exception {
   DiscoveryGroupConfiguration groupConfiguration = new DiscoveryGroupConfiguration().setBroadcastEndpointFactory(new UDPBroadcastEndpointFactory().setGroupAddress(groupAddress).setGroupPort(groupPort));
   ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(groupConfiguration, JMSFactoryType.CF);
   assertFactoryParams(cf, null, groupConfiguration, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
   Connection conn = cf.createConnection();

   conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   testSettersThrowException(cf);

   conn.close();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:14,代码来源:ActiveMQConnectionFactoryTest.java

示例4: testStaticConnectorLiveConstructor

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testStaticConnectorLiveConstructor() throws Exception {
   ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);
   assertFactoryParams(cf, new TransportConfiguration[]{liveTC}, null, null, ActiveMQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD, ActiveMQClient.DEFAULT_CONNECTION_TTL, ActiveMQClient.DEFAULT_CALL_TIMEOUT, ActiveMQClient.DEFAULT_CALL_FAILOVER_TIMEOUT, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, ActiveMQClient.DEFAULT_CONSUMER_WINDOW_SIZE, ActiveMQClient.DEFAULT_CONSUMER_MAX_RATE, ActiveMQClient.DEFAULT_CONFIRMATION_WINDOW_SIZE, ActiveMQClient.DEFAULT_PRODUCER_MAX_RATE, ActiveMQClient.DEFAULT_BLOCK_ON_ACKNOWLEDGE, ActiveMQClient.DEFAULT_BLOCK_ON_DURABLE_SEND, ActiveMQClient.DEFAULT_BLOCK_ON_NON_DURABLE_SEND, ActiveMQClient.DEFAULT_AUTO_GROUP, ActiveMQClient.DEFAULT_PRE_ACKNOWLEDGE, ActiveMQClient.DEFAULT_CONNECTION_LOAD_BALANCING_POLICY_CLASS_NAME, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE, ActiveMQClient.DEFAULT_DISCOVERY_INITIAL_WAIT_TIMEOUT, ActiveMQClient.DEFAULT_USE_GLOBAL_POOLS, ActiveMQClient.DEFAULT_SCHEDULED_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_THREAD_POOL_MAX_SIZE, ActiveMQClient.DEFAULT_RETRY_INTERVAL, ActiveMQClient.DEFAULT_RETRY_INTERVAL_MULTIPLIER, ActiveMQClient.DEFAULT_RECONNECT_ATTEMPTS);
   Connection conn = cf.createConnection();

   conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   testSettersThrowException(cf);

   cf.close();

   conn.close();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:15,代码来源:ActiveMQConnectionFactoryTest.java

示例5: sendObjectMessage

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
private void sendObjectMessage(String qname, Serializable obj) throws Exception {
   ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://0");
   Connection connection = factory.createConnection();
   try {
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue q = session.createQueue(qname);
      MessageProducer producer = session.createProducer(q);
      ObjectMessage objMessage = session.createObjectMessage();
      objMessage.setObject(obj);
      producer.send(objMessage);
   } finally {
      connection.close();
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:15,代码来源:ActiveMQConnectionFactoryTest.java

示例6: setUp

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
   connectionFactory = new ActiveMQConnectionFactory(jmsServer.getVmURL());
   connection = connectionFactory.createConnection();
   session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   consumer = (ActiveMQMessageConsumer) session.createConsumer(ActiveMQDestination.createDestination(TEST_TOPIC, ActiveMQDestination.TYPE.TOPIC));
   connection.start();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:9,代码来源:EmbeddedJMSResourceSingleFileConfigurationTest.java

示例7: testPlain

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testPlain() throws Exception {
   ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
   Connection connection = cf.createConnection();
   Session session = connection.createSession();
   MessageProducer producer = session.createProducer(session.createQueue("queue"));
   producer.send(session.createTextMessage("hello"));
   connection.start();
   MessageConsumer consumer = session.createConsumer(session.createQueue("queue"));
   Assert.assertNotNull(consumer.receive(5000));
   connection.close();

}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:14,代码来源:PlainTest.java

示例8: setUp

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
   connectionFactory = new ActiveMQConnectionFactory(jmsServer.getVmURL());
   connection = connectionFactory.createConnection();
   session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   consumer = session.createConsumer(ActiveMQDestination.createDestination(TEST_DESTINATION_NAME, ActiveMQDestination.TYPE.TOPIC));
   connection.start();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:9,代码来源:EmbeddedJMSResourceTopicTest.java

示例9: testCloseCFOnGC

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testCloseCFOnGC() throws Exception {

   final AtomicInteger valueGC = new AtomicInteger(0);

   ServerLocatorImpl.finalizeCallback = new Runnable() {
      @Override
      public void run() {
         valueGC.incrementAndGet();
      }
   };

   try {
      // System.setOut(out);
      for (int i = 0; i < 100; i++) {
         ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
         Connection conn = cf.createConnection();
         cf = null;
         conn.close();
         conn = null;
      }
      forceGC();
   } finally {
      ServerLocatorImpl.finalizeCallback = null;
   }

   assertEquals("The code is throwing exceptions", 0, valueGC.get());

}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:30,代码来源:CloseConnectionFactoryOnGCTest.java

示例10: start

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
void start() {
   connectionFactory = new ActiveMQConnectionFactory(getVmURL());
   try {
      connection = connectionFactory.createConnection();
      session = connection.createSession();
      producer = session.createProducer(null);
      connection.start();
   } catch (JMSException jmsEx) {
      throw new EmbeddedJMSResourceException("InternalClient creation failure", jmsEx);
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:12,代码来源:EmbeddedJMSResource.java

示例11: AccConnectionManager

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
AccConnectionManager(final ClientOptions clientOptions, String brokerUrl) {
        super();
        if (clientOptions.getOption(ClientOptions.USERNAME).hasParsedValue()) {
            username = clientOptions.getOption(ClientOptions.USERNAME).getValue();
        }
        if (clientOptions.getOption(ClientOptions.PASSWORD).hasParsedValue()) {
            password = clientOptions.getOption(ClientOptions.PASSWORD).getValue();
        }

        // GOTCHA: this is more or less unrelated to initialContext field
//        setInitialContext(brokerUrl);
        factory = new ActiveMQConnectionFactory(brokerUrl);

        try {
            if (clientOptions.getOption(ClientOptions.DESTINATION_TYPE).getValue().equals(TOPIC_OBJECT)) {
                destination = createTopic(clientOptions.getOption(ClientOptions.ADDRESS).getValue());
            } else if (clientOptions.getOption(ClientOptions.DESTINATION_TYPE).getValue().equals(QUEUE_OBJECT)) {
                destination = createQueue(clientOptions.getOption(ClientOptions.ADDRESS).getValue());
            } else {
                // reserved for future other destination types
                LOG.warn("Not sure what type of Destination to create. Falling back to Destination");
                throw new RuntimeException("WTF?");
////                destination = (Destination) initialContext.lookup(this.queueOrTopic);
            }

            LOG.trace("Destination=" + destination);
            if (clientOptions.getOption(ClientOptions.BROKER_URI).hasParsedValue() || (username == null && password == null)) {
                connection = factory.createConnection();
            } else {
                LOG.trace("Using credentials " + username + ":" + password);
                connection = factory.createConnection(username, password);
            }

            final MessagingExceptionListener exceptionListener = new MessagingExceptionListener();
            connection.setExceptionListener(new ExceptionListener() {
                @Override
                public void onException(JMSException exception) {
                    if (clientOptions.getOption(ClientOptions.CON_RECONNECT).getValue().matches("[tT]rue") &&
                        (exception.getCause() instanceof ActiveMQDisconnectedException ||
                            exception.getCause() instanceof ActiveMQNotConnectedException)) {
                        return;
                    }
                    exceptionListener.onException(exception);
                }
            });
        } catch (JMSException e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
 
开发者ID:rh-messaging,项目名称:cli-java,代码行数:52,代码来源:AccConnectionManager.java

示例12: main

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
   final Map<String, String> messageReceiverMap = new ConcurrentHashMap<>();
   Connection connection = null;
   try {

      // Step 2. Perform a lookup on the queue
      Queue queue = ActiveMQJMSClient.createQueue("queue/exampleQueue");

      // Step 3. Perform a lookup on the Connection Factory
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();

      // Step 4. Create a JMS Connection
      connection = cf.createConnection();

      // Step 5. Create a JMS Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 6. Create a JMS Message Producer
      MessageProducer producer = session.createProducer(queue);

      // Step 7. Create two consumers
      MessageConsumer consumer1 = session.createConsumer(queue);
      consumer1.setMessageListener(new SimpleMessageListener("consumer-1", messageReceiverMap));
      MessageConsumer consumer2 = session.createConsumer(queue);
      consumer2.setMessageListener(new SimpleMessageListener("consumer-2", messageReceiverMap));

      // Step 8. Create and send 10 text messages with group id 'Group-0'
      int msgCount = 10;
      TextMessage[] groupMessages = new TextMessage[msgCount];
      for (int i = 0; i < msgCount; i++) {
         groupMessages[i] = session.createTextMessage("Group-0 message " + i);
         groupMessages[i].setStringProperty("JMSXGroupID", "Group-0");
         producer.send(groupMessages[i]);
         System.out.println("Sent message: " + groupMessages[i].getText());
      }

      System.out.println("all messages are sent");

      // Step 9. Start the connection
      connection.start();

      Thread.sleep(2000);

      // Step 10. check the group messages are received by only one consumer
      String trueReceiver = messageReceiverMap.get(groupMessages[0].getText());
      for (TextMessage grpMsg : groupMessages) {
         String receiver = messageReceiverMap.get(grpMsg.getText());
         if (!trueReceiver.equals(receiver)) {
            throw new IllegalStateException("Group message [" + grpMsg.getText() + "[ went to wrong receiver: " + receiver);
         }
      }

      cf.close();
   } finally {
      // Step 11. Be sure to close our JMS resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:61,代码来源:MessageGroupExample.java

示例13: main

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
   final Map<String, String> messageReceiverMap = new ConcurrentHashMap<>();
   Connection connection = null;
   try {
      //Step 2. Perform a lookup on the queue
      Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");

      //Step 3. Perform a lookup on the Connection Factory
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?groupID=Group-0");

      //Step 4. Create a JMS Connection
      connection = cf.createConnection();

      //Step 5. Create a JMS Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      //Step 6. Create 2 JMS Message Producers
      MessageProducer producer1 = session.createProducer(queue);

      MessageProducer producer2 = session.createProducer(queue);

      //Step 7. Create two consumers
      MessageConsumer consumer1 = session.createConsumer(queue);
      consumer1.setMessageListener(new SimpleMessageListener("consumer-1", messageReceiverMap));
      MessageConsumer consumer2 = session.createConsumer(queue);
      consumer2.setMessageListener(new SimpleMessageListener("consumer-2", messageReceiverMap));

      //Step 8. Create and send 10 text messages with each producer
      int msgCount = 10;
      for (int i = 0; i < msgCount; i++) {
         TextMessage m = session.createTextMessage("producer1 message " + i);
         producer1.send(m);
         System.out.println("Sent message: " + m.getText());
         TextMessage m2 = session.createTextMessage("producer2 message " + i);
         producer2.send(m2);
         System.out.println("Sent message: " + m2.getText());
      }

      System.out.println("all messages are sent");

      //Step 9. Start the connection
      connection.start();

      Thread.sleep(2000);

      //Step 10. check the group messages are received by only one consumer

      String trueReceiver = messageReceiverMap.get("producer1 message " + 0);
      for (int i = 0; i < msgCount; i++) {
         String receiver = messageReceiverMap.get("producer1 message " + i);
         if (!trueReceiver.equals(receiver)) {
            throw new IllegalStateException("Group message [producer1 message " + i + "] went to wrong receiver: " + receiver);
         }
         receiver = messageReceiverMap.get("producer2 message " + i);
         if (!trueReceiver.equals(receiver)) {
            throw new IllegalStateException("Group message [producer2 message " + i + "] went to wrong receiver: " + receiver);
         }
      }
   } finally {
      //Step 11. Be sure to close our JMS resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:66,代码来源:MessageGroup2Example.java

示例14: main

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
   Connection connection = null;
   try {
      // Step 2. new Queue
      Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");

      // Step 3. new Connection Factory
      ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();

      // Step 4.Create a JMS Connection, session and producer on the queue
      connection = cf.createConnection();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer producer = session.createProducer(queue);

      // Step 5. Create and send a text message with the Last-Value header set
      TextMessage message = session.createTextMessage("1st message with Last-Value property set");
      message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
      producer.send(message);
      System.out.format("Sent message: %s%n", message.getText());

      // Step 6. Create and send a second text message with the Last-Value header set
      message = session.createTextMessage("2nd message with Last-Value property set");
      message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
      producer.send(message);
      System.out.format("Sent message: %s%n", message.getText());

      // Step 7. Create and send a third text message with the Last-Value header set
      message = session.createTextMessage("3rd message with Last-Value property set");
      message.setStringProperty("_AMQ_LVQ_NAME", "STOCK_NAME");
      producer.send(message);
      System.out.format("Sent message: %s%n", message.getText());

      // Step 8. Browse the queue. There is only 1 message in it, the last sent
      try (QueueBrowser browser = session.createBrowser(queue)) {
         Enumeration enumeration = browser.getEnumeration();
         while (enumeration.hasMoreElements()) {
            TextMessage messageInTheQueue = (TextMessage) enumeration.nextElement();
            System.out.format("Message in the queue: %s%n", messageInTheQueue.getText());
         }
      }

      // Step 9. Create a JMS Message Consumer for the queue
      MessageConsumer messageConsumer = session.createConsumer(queue);

      // Step 10. Start the Connection
      connection.start();

      // Step 11. Trying to receive a message. Since the queue is configured to keep only the
      // last message with the Last-Value header set, the message received is the last sent
      TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
      System.out.format("Received message: %s%n", messageReceived.getText());

      // Step 12. Trying to receive another message but there will be none.
      // The 1st message was discarded when the 2nd was sent to the queue.
      // The 2nd message was in turn discarded when the 3trd was sent to the queue
      messageReceived = (TextMessage) messageConsumer.receive(5000);
      System.out.format("Received message: %s%n", messageReceived);

      cf.close();
   } finally {
      // Step 13. Be sure to close our JMS resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:67,代码来源:LastValueQueueExample.java

示例15: testRemoteCFWithTCPUserPassword

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; //导入方法依赖的package包/类
@Test
public void testRemoteCFWithTCPUserPassword() throws Exception {

   //setup user and role on broker
   ((ActiveMQJAASSecurityManager) liveService.getSecurityManager()).getConfiguration().addUser("myUser", "myPassword");
   ((ActiveMQJAASSecurityManager) liveService.getSecurityManager()).getConfiguration().addRole("myUser", "consumeCreateRole");
   Role consumeCreateRole = new Role("consumeCreateRole", false, true, true, true, true, true, true, true);
   Set<Role> consumerCreateRoles = new HashSet<>();
   consumerCreateRoles.add(consumeCreateRole);
   liveService.getSecurityRepository().addMatch("test.queue", consumerCreateRoles);

   Hashtable<String, String> props = new Hashtable<>();
   props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");

   //user and password set on URL
   props.put("connectionFactory.myConnectionFactory", "tcp://127.0.0.1:61616?user=myUser&password=myPassword");
   Context ctx = new InitialContext(props);

   //create a connection factory
   ActiveMQConnectionFactory connectionFactory = (ActiveMQConnectionFactory) ctx.lookup("myConnectionFactory");
   Assert.assertEquals("ensure user is set", "myUser", connectionFactory.getUser());
   Assert.assertEquals("ensure password is set", "myPassword", connectionFactory.getPassword());

   //Connect to broker to verify credentials are used with connection
   Connection connection = connectionFactory.createConnection();
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Queue queue = session.createQueue("test.queue");

   try {

      try {
         MessageProducer producer = session.createProducer(queue);
         producer.send(session.createTextMessage("test Msg"));
         Assert.fail("Sending message should throw a JMSSecurityException");
      } catch (JMSSecurityException e) {
         //expected
      }

      MessageConsumer consumer = session.createConsumer(queue);
   } finally {
      connection.close();
   }

}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:47,代码来源:SimpleJNDIClientTest.java


注:本文中的org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。