/** * A static helper method to create a new connection * * @param uri * @return and ActiveMQConnection * @throws JMSException */ public static ActiveMQConnection makeConnection(String uri) throws JMSException, URISyntaxException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(uri); return (ActiveMQConnection)factory.createConnection(); }
ActiveMQDestination dest = destination.getActiveMQDestination(); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUrl); Connection connection = null; try { connection = cf.createConnection(userName, password); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(dest); ActiveMQTextMessage msg = (ActiveMQTextMessage) session.createTextMessage(body); producer.send(msg); connection.close();
@Test(timeout = 60000) public void testStealLinkFails() throws Exception { final String clientID = "ThisIsAClientId"; ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(unstealableConnectionURI); Connection connection1 = factory.createConnection(); connection1.setClientID(clientID); connection1.start(); try { Connection connection2 = factory.createConnection(); connection2.setClientID(clientID); connection2.start(); fail("Should not have been able to steal the link."); } catch (InvalidClientIDException e) { LOG.info("Caught expected error on trying to steal link: {}", e.getMessage()); LOG.trace("Error: ", e); } }
@Test public void testSimple() throws Exception { Connection connection = factory.createConnection(); Collection<Session> sessions = new LinkedList<>(); for (int i = 0; i < 10; i++) { Session session = connection.createSession(true, Session.SESSION_TRANSACTED); sessions.add(session); } connection.close(); }
@Override public synchronized void open() throws JMSException { if (isClosed()) throw new JMSException("connection closed"); if (connection == null) { log().i("connect",url); Connection con = connectionFactory.createConnection(); con.start(); connection = con; connection.setExceptionListener(this); session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); } }
private static EventService buildJmsPublisher(final NotificationsConfiguration config, final Environment environment) throws JMSException { LOGGER.info("Connecting to JMS broker at {}", config.getConnectionString()); final Connection jmsConnection = getJmsFactory(config).createConnection(); environment.lifecycle().manage(new AutoCloseableManager(jmsConnection)); return new JmsPublisher(jmsConnection.createSession(false, AUTO_ACKNOWLEDGE), config.getTopicName()); }
@Override @Before public void setUp() throws Exception { super.setUp(); startedConnection = factory.createConnection(); startedConnection.start(); stoppedConnection = factory.createConnection(); }
@Before public void setUp() throws Exception { brokerService = new BrokerService(); brokerService.setPersistent(false); brokerService.start(); factory = new ActiveMQConnectionFactory(BrokerRegistry.getInstance().findFirst().getVmConnectorURI()); producerConnection = factory.createConnection(); producerConnection.start(); producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); queue = producerSession.createQueue(getClass().getName()); producer = producerSession.createProducer(queue); }
@Test(timeout = 60000) public void testStealLinkSuccess() throws Exception { final String clientID = "ThisIsAClientId"; ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(stealableConnectionURI); Connection connection1 = factory.createConnection(); connection1.setClientID(clientID); connection1.start(); Connection connection2 = factory.createConnection(); connection2.setClientID(clientID); connection2.start(); } catch (InvalidClientIDException e) { LOG.info("Should not have failed while stealing the link: {}", e.getMessage());
@Test public void testInternalSessionHandling() throws Exception { try (Connection conn = factory.createConnection()) { conn.start(); try (Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)) { Destination dest = createDestination(session,ActiveMQDestination.QUEUE_TYPE); sendMessages(session, dest, 1); MessageConsumer consumer = session.createConsumer(dest); Message m = consumer.receive(2000); assertNotNull(m); } } assertFalse(AssertionLoggerHandler.findText("Client connection failed, clearing up resources for session")); assertFalse(AssertionLoggerHandler.findText("Cleared up resources for session")); }
/** * A static helper method to create a new connection * * @return an ActiveMQConnection * @throws JMSException */ public static ActiveMQConnection makeConnection() throws JMSException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); return (ActiveMQConnection)factory.createConnection(); }
@Override protected void setUp() throws Exception { brokerService = new BrokerService(); brokerService.setPersistent(false); brokerService.start(); factory = new ActiveMQConnectionFactory(BrokerRegistry.getInstance().findFirst().getVmConnectorURI()); consumerConnection = factory.createConnection(); consumerConnection.start(); producerConnection = factory.createConnection(); producerConnection.start(); consumerSession = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); topic = consumerSession.createTopic(getName()); producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); consumer = consumerSession.createConsumer(topic); producer = producerSession.createProducer(topic); }
@Test public void testAMQ5050DefaultHost() throws Exception { // Let verify a host header is added to the connection. Connection connection = new ActiveMQConnectionFactory(brokerConnectURI).createConnection(); connection.start(); CopyOnWriteArrayList<TransportConnection> connections = broker.getConnectorByName("tcp").getConnections(); assertEquals(1, connections.size()); assertNotNull(connections.get(0).getRemoteWireFormatInfo().getHost()); connection.stop(); }
/** * A static helper method to create a new connection * * @param user * @param password * @param uri * @return an ActiveMQConnection * @throws JMSException */ public static ActiveMQConnection makeConnection(String user, String password, String uri) throws JMSException, URISyntaxException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user, password, new URI(uri)); return (ActiveMQConnection)factory.createConnection(); }
@Before public void setUp() throws Exception { brokerService = new BrokerService(); brokerService.setPersistent(false); brokerService.setUseJmx(true); String connectionUri = brokerService.addConnector("tcp://localhost:0").getPublishableConnectString(); brokerService.start(); brokerService.waitUntilStarted(); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(connectionUri); final Connection conn = connectionFactory.createConnection(); try { conn.start(); final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); final Destination queue = session.createQueue(testQueueName); final Message toSend = session.createMessage(); toSend.setStringProperty("foo", "bar"); final MessageProducer producer = session.createProducer(queue); producer.send(queue, toSend); } finally { conn.close(); } }
@Test public void testAMQ5050WithManualSpecifiedHost() throws Exception { // Let verify a host header is added to the connection. Connection connection = new ActiveMQConnectionFactory(brokerConnectURI+"?wireFormat.host=foo").createConnection(); connection.start(); CopyOnWriteArrayList<TransportConnection> connections = broker.getConnectorByName("tcp").getConnections(); assertEquals(1, connections.size()); assertEquals("foo", connections.get(0).getRemoteWireFormatInfo().getHost()); connection.stop(); }
@Autowired public ActiveMqMessagingService(DaemonThreadStatsCollector daemonThreadStatsCollector) throws Exception { this.daemonThreadStatsCollector = daemonThreadStatsCollector; SystemEnvironment systemEnvironment = new SystemEnvironment(); broker = new BrokerService(); broker.setBrokerName(BROKER_NAME); broker.setPersistent(false); broker.setUseJmx(systemEnvironment.getActivemqUseJmx()); broker.getManagementContext().setConnectorPort(systemEnvironment.getActivemqConnectorPort()); broker.start(); factory = new ActiveMQConnectionFactory(BROKER_URL); factory.getPrefetchPolicy().setQueuePrefetch(systemEnvironment.getActivemqQueuePrefetch()); factory.setCopyMessageOnSend(false); connection = (ActiveMQConnection) factory.createConnection(); connection.start(); }
public static void main(String[] args) throws Exception { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("TEST.QUEUE"); MessageProducer producer = session.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); for (int i= 0; i < 100; i++) { TextMessage message = session.createTextMessage("hello world! " + i); producer.send(message); System.out.println(message); } producer.close(); } }
protected Connection startJmsConnection(String url) { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connectionFactory.setAlwaysSyncSend(false); Connection connect = connectionFactory.createConnection(); connect.start(); return connect; } catch (JMSException e) { throw new AssertionError("Failed to establish the JMS-Connection!", e); } } }
/** * A static helper method to create a new connection * * @param uri * @return and ActiveMQConnection * @throws JMSException */ public static ActiveMQConnection makeConnection(String uri) throws JMSException, URISyntaxException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(uri); return (ActiveMQConnection)factory.createConnection(); }