Here are the examples of the java api class org.apache.activemq.broker.ConnectionContext taken from open source projects.
1. LargeQueueSparseDeleteTest#testRemoveMessages()
Project: activemq-artemis
File: LargeQueueSparseDeleteTest.java
File: LargeQueueSparseDeleteTest.java
public void testRemoveMessages() throws Exception { final int QUEUE_SIZE = 30000; final long TEST_TIMEOUT = 20000; // Populate a test queue with uniquely-identifiable messages. Connection conn = createConnection(); try { conn.start(); Session session = conn.createSession(true, Session.SESSION_TRANSACTED); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < QUEUE_SIZE; i++) { Message message = session.createMessage(); message.setIntProperty("id", i); producer.send(message); } session.commit(); } finally { conn.close(); } // Access the implementation of the test queue and move the last message // to another queue. Verify that the move occurred within the limits of // the test. Queue queue = (Queue) broker.getRegionBroker().getDestinationMap().get(destination); ConnectionContext context = new ConnectionContext(new NonCachedMessageEvaluationContext()); context.setBroker(broker.getBroker()); context.getMessageEvaluationContext().setDestination(destination); long startTimeMillis = System.currentTimeMillis(); Assert.assertEquals(1, queue.removeMatchingMessages("id=" + (QUEUE_SIZE - 1))); long durationMillis = System.currentTimeMillis() - startTimeMillis; LOG.info("It took " + durationMillis + "ms to remove the last message from a queue a " + QUEUE_SIZE + " messages."); Assert.assertTrue("Removing the message took too long: " + durationMillis + "ms", durationMillis < TEST_TIMEOUT); }
2. LargeQueueSparseDeleteTest#testCopyMessages()
Project: activemq-artemis
File: LargeQueueSparseDeleteTest.java
File: LargeQueueSparseDeleteTest.java
public void testCopyMessages() throws Exception { final int QUEUE_SIZE = 30000; final String MOVE_TO_DESTINATION_NAME = getDestinationString() + ".dest"; final long TEST_TIMEOUT = 10000; // Populate a test queue with uniquely-identifiable messages. Connection conn = createConnection(); try { conn.start(); Session session = conn.createSession(true, Session.SESSION_TRANSACTED); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < QUEUE_SIZE; i++) { Message message = session.createMessage(); message.setIntProperty("id", i); producer.send(message); } session.commit(); } finally { conn.close(); } // Access the implementation of the test queue and move the last message // to another queue. Verify that the move occurred within the limits of // the test. Queue queue = (Queue) broker.getRegionBroker().getDestinationMap().get(destination); ConnectionContext context = new ConnectionContext(new NonCachedMessageEvaluationContext()); context.setBroker(broker.getBroker()); context.getMessageEvaluationContext().setDestination(destination); long startTimeMillis = System.currentTimeMillis(); Assert.assertEquals(1, queue.copyMatchingMessagesTo(context, "id=" + (QUEUE_SIZE - 1), createDestination(MOVE_TO_DESTINATION_NAME))); long durationMillis = System.currentTimeMillis() - startTimeMillis; LOG.info("It took " + durationMillis + "ms to copy the last message from a queue a " + QUEUE_SIZE + " messages."); Assert.assertTrue("Copying the message took too long: " + durationMillis + "ms", durationMillis < TEST_TIMEOUT); }
3. LargeQueueSparseDeleteTest#testMoveMessages()
Project: activemq-artemis
File: LargeQueueSparseDeleteTest.java
File: LargeQueueSparseDeleteTest.java
/** * The test queue is filled with QUEUE_SIZE test messages, each with a * numeric id property beginning at 0. Once the queue is filled, the last * message (id = QUEUE_SIZE-1) is moved to another queue. The test succeeds * if the move completes within TEST_TIMEOUT milliseconds. * * @throws Exception */ public void testMoveMessages() throws Exception { final int QUEUE_SIZE = 30000; final String MOVE_TO_DESTINATION_NAME = getDestinationString() + ".dest"; final long TEST_TIMEOUT = 20000; // Populate a test queue with uniquely-identifiable messages. Connection conn = createConnection(); try { conn.start(); Session session = conn.createSession(true, Session.SESSION_TRANSACTED); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < QUEUE_SIZE; i++) { Message message = session.createMessage(); message.setIntProperty("id", i); producer.send(message); } session.commit(); } finally { conn.close(); } // Access the implementation of the test queue and move the last message // to another queue. Verify that the move occurred within the limits of // the test. Queue queue = (Queue) broker.getRegionBroker().getDestinationMap().get(destination); ConnectionContext context = new ConnectionContext(new NonCachedMessageEvaluationContext()); context.setBroker(broker.getBroker()); context.getMessageEvaluationContext().setDestination(destination); long startTimeMillis = System.currentTimeMillis(); Assert.assertEquals(1, queue.moveMatchingMessagesTo(context, "id=" + (QUEUE_SIZE - 1), createDestination(MOVE_TO_DESTINATION_NAME))); long durationMillis = System.currentTimeMillis() - startTimeMillis; LOG.info("It took " + durationMillis + "ms to move the last message from a queue a " + QUEUE_SIZE + " messages."); Assert.assertTrue("Moving the message took too long: " + durationMillis + "ms", durationMillis < TEST_TIMEOUT); }
4. QueueOptimizedDispatchExceptionTest#TestOptimizedDispatchCME()
Project: activemq-artemis
File: QueueOptimizedDispatchExceptionTest.java
File: QueueOptimizedDispatchExceptionTest.java
@Test public void TestOptimizedDispatchCME() throws Exception { final PersistenceAdapter persistenceAdapter = broker.getPersistenceAdapter(); final MessageStore queueMessageStore = persistenceAdapter.createQueueMessageStore(destination); final ConnectionContext contextNotInTx = new ConnectionContext(); contextNotInTx.setConnection(new Connection() { @Override public void stop() throws Exception { } @Override public void start() throws Exception { } @Override public void updateClient(ConnectionControl control) { } @Override public void serviceExceptionAsync(IOException e) { } @Override public void serviceException(Throwable error) { } @Override public Response service(Command command) { return null; } @Override public boolean isSlow() { return false; } @Override public boolean isNetworkConnection() { return false; } @Override public boolean isManageable() { return false; } @Override public boolean isFaultTolerantConnection() { return false; } @Override public boolean isConnected() { return true; } @Override public boolean isBlocked() { return false; } @Override public boolean isActive() { return false; } @Override public ConnectionStatistics getStatistics() { return null; } @Override public String getRemoteAddress() { return null; } @Override public int getDispatchQueueSize() { return 0; } @Override public Connector getConnector() { // TODO Auto-generated method stub return null; } @Override public String getConnectionId() { return null; } @Override public void dispatchSync(Command message) { } @Override public void dispatchAsync(Command command) { } @Override public int getActiveTransactionCount() { return 0; } @Override public Long getOldestActiveTransactionDuration() { return null; } }); final DestinationStatistics destinationStatistics = new DestinationStatistics(); final Queue queue = new Queue(broker, destination, queueMessageStore, destinationStatistics, broker.getTaskRunnerFactory()); final MockMemoryUsage usage = new MockMemoryUsage(); queue.setOptimizedDispatch(true); queue.initialize(); queue.start(); queue.memoryUsage = usage; ProducerBrokerExchange producerExchange = new ProducerBrokerExchange(); ProducerInfo producerInfo = new ProducerInfo(); ProducerState producerState = new ProducerState(producerInfo); producerExchange.setProducerState(producerState); producerExchange.setConnectionContext(contextNotInTx); // populate the queue store, exceed memory limit so that cache is disabled for (int i = 0; i < count; i++) { Message message = getMessage(i); queue.send(producerExchange, message); } usage.setFull(false); try { queue.wakeup(); } catch (Exception e) { LOG.error("Queue threw an unexpected exception: " + e.toString()); fail("Should not throw an exception."); } }
5. XBeanConfigTest#setUp()
Project: activemq-artemis
File: XBeanConfigTest.java
File: XBeanConfigTest.java
@Override protected void setUp() throws Exception { System.setProperty("brokername", "testbroker"); brokerService = createBroker(); broker = brokerService.getBroker(); // started automatically // brokerService.start(); context = new ConnectionContext(); context.setBroker(broker); info = new ConnectionInfo(); info.setClientId("James"); info.setUserName("James"); info.setConnectionId(new ConnectionId("1234")); try { broker.addConnection(context, info); } catch (Throwable e) { e.printStackTrace(); fail(e.getMessage()); } assertNotNull("No broker created!"); }
6. JaasDualAuthenticationBrokerTest#testInsecureConnector()
Project: activemq-artemis
File: JaasDualAuthenticationBrokerTest.java
File: JaasDualAuthenticationBrokerTest.java
public void testInsecureConnector() { Connector connector = new TransportConnector(nonSslTransportServer); connectionContext.setConnector(connector); connectionInfo.setUserName(INSECURE_USERNAME); try { authBroker.addConnection(connectionContext, connectionInfo); } catch (Exception e) { fail("Call to addConnection failed: " + e.getMessage()); } assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size()); ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext; assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", INSECURE_USERNAME, receivedContext.getSecurityContext().getUserName()); Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals(); assertEquals("2 Principals received", 2, receivedPrincipals.size()); for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) { Principal currentPrincipal = iter.next(); if (currentPrincipal instanceof UserPrincipal) { assertEquals("UserPrincipal is '" + INSECURE_USERNAME + "'", INSECURE_USERNAME, currentPrincipal.getName()); } else if (currentPrincipal instanceof GroupPrincipal) { assertEquals("GroupPrincipal is '" + INSECURE_GROUP + "'", INSECURE_GROUP, currentPrincipal.getName()); } else { fail("Unexpected Principal subclass found."); } } try { authBroker.removeConnection(connectionContext, connectionInfo, null); } catch (Exception e) { fail("Call to removeConnection failed: " + e.getMessage()); } assertEquals("Number of removeConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.removeConnectionData.size()); }
7. JaasDualAuthenticationBrokerTest#testSecureConnector()
Project: activemq-artemis
File: JaasDualAuthenticationBrokerTest.java
File: JaasDualAuthenticationBrokerTest.java
public void testSecureConnector() { Connector connector = new TransportConnector(sslTransportServer); connectionContext.setConnector(connector); connectionInfo.setTransportContext(new StubX509Certificate[] {}); try { authBroker.addConnection(connectionContext, connectionInfo); } catch (Exception e) { fail("Call to addConnection failed: " + e.getMessage()); } assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size()); ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext; assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", DN_USERNAME, receivedContext.getSecurityContext().getUserName()); Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals(); assertEquals("2 Principals received", 2, receivedPrincipals.size()); for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) { Principal currentPrincipal = iter.next(); if (currentPrincipal instanceof UserPrincipal) { assertEquals("UserPrincipal is '" + DN_USERNAME + "'", DN_USERNAME, currentPrincipal.getName()); } else if (currentPrincipal instanceof GroupPrincipal) { assertEquals("GroupPrincipal is '" + DN_GROUP + "'", DN_GROUP, currentPrincipal.getName()); } else { fail("Unexpected Principal subclass found."); } } try { authBroker.removeConnection(connectionContext, connectionInfo, null); } catch (Exception e) { fail("Call to removeConnection failed: " + e.getMessage()); } assertEquals("Number of removeConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.removeConnectionData.size()); }
8. JaasDualAuthenticationBrokerTest#setUp()
Project: activemq-artemis
File: JaasDualAuthenticationBrokerTest.java
File: JaasDualAuthenticationBrokerTest.java
@Override protected void setUp() throws Exception { receiveBroker = new StubBroker(); authBroker = new JaasDualAuthenticationBroker(receiveBroker, "activemq-domain", "activemq-ssl-domain"); connectionContext = new ConnectionContext(); SSLServerSocket sslServerSocket = new StubSSLServerSocket(); StubSSLSocketFactory socketFactory = new StubSSLSocketFactory(sslServerSocket); try { sslTransportServer = new SslTransportServer(null, new URI("ssl://localhost:61616?needClientAuth=true"), socketFactory); } catch (Exception e) { fail("Unable to create SslTransportServer."); } sslTransportServer.setNeedClientAuth(true); sslTransportServer.bind(); try { nonSslTransportServer = new TcpTransportServer(null, new URI("tcp://localhost:61613"), socketFactory); } catch (Exception e) { fail("Unable to create TcpTransportServer."); } connectionInfo = new ConnectionInfo(); createLoginConfig(); }
9. JaasCertificateAuthenticationBrokerTest#testAddConnectionSuccess()
Project: activemq-artemis
File: JaasCertificateAuthenticationBrokerTest.java
File: JaasCertificateAuthenticationBrokerTest.java
public void testAddConnectionSuccess() { String dnUserName = "dnUserName"; HashSet<String> userNames = new HashSet<>(); userNames.add(dnUserName); HashSet<String> groupNames = new HashSet<>(); groupNames.add("testGroup1"); groupNames.add("testGroup2"); groupNames.add("tesetGroup3"); setConfiguration(userNames, groupNames, true); try { authBroker.addConnection(connectionContext, connectionInfo); } catch (Exception e) { fail("Call to addConnection failed: " + e.getMessage()); } assertEquals("Number of addConnection calls to underlying Broker must match number of calls made to " + "AuthenticationBroker.", 1, receiveBroker.addConnectionData.size()); ConnectionContext receivedContext = receiveBroker.addConnectionData.getFirst().connectionContext; assertEquals("The SecurityContext's userName must be set to that of the UserPrincipal.", dnUserName, receivedContext.getSecurityContext().getUserName()); Set<Principal> receivedPrincipals = receivedContext.getSecurityContext().getPrincipals(); for (Iterator<Principal> iter = receivedPrincipals.iterator(); iter.hasNext(); ) { Principal currentPrincipal = iter.next(); if (currentPrincipal instanceof UserPrincipal) { if (userNames.remove(currentPrincipal.getName())) { // Nothing, we did good. } else { // Found an unknown userName. fail("Unknown UserPrincipal found"); } } else if (currentPrincipal instanceof GroupPrincipal) { if (groupNames.remove(currentPrincipal.getName())) { // Nothing, we did good. } else { fail("Unknown GroupPrincipal found."); } } else { fail("Unexpected Principal subclass found."); } } if (!userNames.isEmpty()) { fail("Some usernames were not added as UserPrincipals"); } if (!groupNames.isEmpty()) { fail("Some group names were not added as GroupPrincipals"); } }
10. JaasCertificateAuthenticationBrokerTest#setUp()
Project: activemq-artemis
File: JaasCertificateAuthenticationBrokerTest.java
File: JaasCertificateAuthenticationBrokerTest.java
@Override protected void setUp() throws Exception { receiveBroker = new StubBroker(); authBroker = new JaasCertificateAuthenticationBroker(receiveBroker, ""); connectionContext = new ConnectionContext(); connectionInfo = new ConnectionInfo(); connectionInfo.setTransportContext(new StubX509Certificate[] {}); }
11. StoreQueueCursorNoDuplicateTest#testNoDuplicateAfterCacheFullAndReadPast()
Project: activemq-artemis
File: StoreQueueCursorNoDuplicateTest.java
File: StoreQueueCursorNoDuplicateTest.java
public void testNoDuplicateAfterCacheFullAndReadPast() throws Exception { final PersistenceAdapter persistenceAdapter = brokerService.getPersistenceAdapter(); final MessageStore queueMessageStore = persistenceAdapter.createQueueMessageStore(destination); final ConsumerInfo consumerInfo = new ConsumerInfo(); final DestinationStatistics destinationStatistics = new DestinationStatistics(); consumerInfo.setExclusive(true); final Queue queue = new Queue(brokerService, destination, queueMessageStore, destinationStatistics, null); queueMessageStore.start(); queueMessageStore.registerIndexListener(null); QueueStorePrefetch underTest = new QueueStorePrefetch(queue, brokerService.getBroker()); SystemUsage systemUsage = new SystemUsage(); // ensure memory limit is reached systemUsage.getMemoryUsage().setLimit(messageBytesSize * (count + 2)); underTest.setSystemUsage(systemUsage); underTest.setEnableAudit(false); underTest.start(); assertTrue("cache enabled", underTest.isUseCache() && underTest.isCacheEnabled()); final ConnectionContext contextNotInTx = new ConnectionContext(); for (int i = 0; i < count; i++) { ActiveMQTextMessage msg = getMessage(i); msg.setMemoryUsage(systemUsage.getMemoryUsage()); queueMessageStore.addMessage(contextNotInTx, msg); underTest.addMessageLast(msg); } assertTrue("cache is disabled as limit reached", !underTest.isCacheEnabled()); int dequeueCount = 0; underTest.setMaxBatchSize(2); underTest.reset(); while (underTest.hasNext() && dequeueCount < count) { MessageReference ref = underTest.next(); ref.decrementReferenceCount(); underTest.remove(); LOG.info("Received message: {} with body: {}", ref.getMessageId(), ((ActiveMQTextMessage) ref.getMessage()).getText()); assertEquals(dequeueCount++, ref.getMessageId().getProducerSequenceId()); } underTest.release(); assertEquals(count, dequeueCount); }