Search in sources :

Example 46 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class SimpleJNDIClientTest method testRemoteCFWithTCPUserPassword.

@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, 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();
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) MessageConsumer(javax.jms.MessageConsumer) Hashtable(java.util.Hashtable) JMSSecurityException(javax.jms.JMSSecurityException) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) Role(org.apache.activemq.artemis.core.security.Role) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) Queue(javax.jms.Queue) MessageProducer(javax.jms.MessageProducer) HashSet(java.util.HashSet) Session(javax.jms.Session) Test(org.junit.Test)

Example 47 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class URITest method testParseURIs.

@Test
public void testParseURIs() throws Throwable {
    ActiveMQConnectionFactory factory = ActiveMQJMSClient.createConnectionFactory("(tcp://localhost:61616,tcp://localhost:61617)?blockOnDurableSend=false", "some name");
    Assert.assertEquals(2, ((ServerLocatorImpl) factory.getServerLocator()).getInitialConnectors().length);
    ActiveMQConnectionFactory factory2 = new ActiveMQConnectionFactory("(tcp://localhost:61614,tcp://localhost:61616)?blockOnDurableSend=false");
    Assert.assertEquals(2, ((ServerLocatorImpl) factory2.getServerLocator()).getInitialConnectors().length);
    ServerLocator locator = ServerLocatorImpl.newLocator("(tcp://localhost:61616,tcp://localhost:61617)?blockOnDurableSend=false");
    Assert.assertEquals(2, ((ServerLocatorImpl) locator).getInitialConnectors().length);
    ServerLocator locator2 = ActiveMQClient.createServerLocator("(tcp://localhost:61616,tcp://localhost:61617)?blockOnDurableSend=false");
    Assert.assertEquals(2, ((ServerLocatorImpl) locator2).getInitialConnectors().length);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ServerLocatorImpl(org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 48 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ConnectionTest method testTwoConnectionsSameIDThroughCF.

@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();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) InvalidClientIDException(javax.jms.InvalidClientIDException) QueueSession(javax.jms.QueueSession) XASession(javax.jms.XASession) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 49 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class GroupingTest method testGroupingRollbackOnClose.

@Test
public void testGroupingRollbackOnClose() throws Exception {
    ActiveMQConnectionFactory fact = (ActiveMQConnectionFactory) getCF();
    fact.setConsumerWindowSize(1000);
    fact.setTransactionBatchSize(0);
    Connection connection = fact.createConnection();
    RemotingConnection rc = server.getRemotingService().getConnections().iterator().next();
    Connection connection2 = fact.createConnection();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Session session2 = connection2.createSession(true, Session.SESSION_TRANSACTED);
    MessageProducer producer = session.createProducer(queue);
    MessageConsumer consumer1 = session.createConsumer(queue);
    MessageConsumer consumer2 = session2.createConsumer(queue);
    connection.start();
    connection2.start();
    String jmsxgroupID = null;
    for (int j = 0; j < 100; j++) {
        TextMessage message = session.createTextMessage();
        message.setText("Message" + j);
        setProperty(message);
        producer.send(message);
        String prop = message.getStringProperty("JMSXGroupID");
        assertNotNull(prop);
        if (jmsxgroupID != null) {
            assertEquals(jmsxgroupID, prop);
        } else {
            jmsxgroupID = prop;
        }
    }
    session.commit();
    // consume 5 msgs from 1st first consumer
    for (int j = 0; j < 1; j++) {
        TextMessage tm = (TextMessage) consumer1.receive(10000);
        assertNotNull(tm);
        assertEquals("Message" + j, tm.getText());
        assertEquals(tm.getStringProperty("JMSXGroupID"), jmsxgroupID);
    }
    Thread.sleep(2000);
    // session.rollback();
    // session.close();
    // consume all msgs from 2nd first consumer
    // ClientSession amqs = ((ActiveMQSession) session).getCoreSession();
    // ((DelegatingSession) amqs).getChannel().close();
    rc.fail(new ActiveMQNotConnectedException());
    for (int j = 0; j < 10; j++) {
        TextMessage tm = (TextMessage) consumer2.receive(10000);
        assertNotNull(tm);
        long text = ((ActiveMQTextMessage) tm).getCoreMessage().getMessageID();
        System.out.println(tm.getJMSMessageID() + " text = " + text);
        // assertEquals("Message" + j, text);
        assertEquals(tm.getStringProperty("JMSXGroupID"), jmsxgroupID);
    }
    connection.close();
    connection2.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) MessageProducer(javax.jms.MessageProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.artemis.jms.client.ActiveMQTextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 50 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class GroupingTest method testManyGroups.

@Test
public void testManyGroups() throws Exception {
    ConnectionFactory fact = getCF();
    Assume.assumeFalse("only makes sense withOUT auto-group", ((ActiveMQConnectionFactory) fact).isAutoGroup());
    Connection connection = fact.createConnection();
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(queue);
    MessageConsumer consumer1 = session.createConsumer(queue);
    MessageConsumer consumer2 = session.createConsumer(queue);
    MessageConsumer consumer3 = session.createConsumer(queue);
    connection.start();
    for (int j = 0; j < 1000; j++) {
        TextMessage message = session.createTextMessage();
        message.setText("Message" + j);
        message.setStringProperty("_AMQ_GROUP_ID", "" + (j % 10));
        producer.send(message);
        String prop = message.getStringProperty("JMSXGroupID");
        assertNotNull(prop);
    }
    int msg1 = flushMessages(consumer1);
    int msg2 = flushMessages(consumer2);
    int msg3 = flushMessages(consumer3);
    assertNotSame(0, msg1);
    assertNotSame(0, msg2);
    assertNotSame(0, msg2);
    consumer1.close();
    consumer2.close();
    consumer3.close();
    connection.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) MessageProducer(javax.jms.MessageProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.artemis.jms.client.ActiveMQTextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)221 Test (org.junit.Test)141 Connection (javax.jms.Connection)84 Session (javax.jms.Session)84 MessageProducer (javax.jms.MessageProducer)63 MessageConsumer (javax.jms.MessageConsumer)60 TextMessage (javax.jms.TextMessage)49 Queue (javax.jms.Queue)48 ConnectionFactory (javax.jms.ConnectionFactory)35 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)24 ActiveMQResourceAdapter (org.apache.activemq.artemis.ra.ActiveMQResourceAdapter)24 URI (java.net.URI)22 JMSException (javax.jms.JMSException)20 Message (javax.jms.Message)19 DiscoveryGroupConfiguration (org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration)19 InitialContext (javax.naming.InitialContext)16 Context (javax.naming.Context)15 Hashtable (java.util.Hashtable)14