Search in sources :

Example 16 with Interceptor

use of org.apache.activemq.artemis.api.core.Interceptor in project activemq-artemis by apache.

the class JMSFailoverTest method testSendReceiveLargeMessages.

@Test
public void testSendReceiveLargeMessages() throws Exception {
    SimpleString QUEUE = new SimpleString("somequeue");
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc, backuptc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    jbcf.setMinLargeMessageSize(1024);
    // jbcf.setConsumerWindowSize(0);
    // jbcf.setMinLargeMessageSize(1024);
    final CountDownLatch flagAlign = new CountDownLatch(1);
    final CountDownLatch waitToKill = new CountDownLatch(1);
    final AtomicBoolean killed = new AtomicBoolean(false);
    jbcf.getServerLocator().addIncomingInterceptor(new Interceptor() {

        int count = 0;

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            if (packet instanceof SessionReceiveContinuationMessage) {
                if (count++ == 300 && !killed.get()) {
                    System.out.println("sending countDown on latch waitToKill");
                    killed.set(true);
                    waitToKill.countDown();
                }
            }
            return true;
        }
    });
    Connection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
    final ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    // The thread that will fail the server
    Thread spoilerThread = new Thread() {

        @Override
        public void run() {
            flagAlign.countDown();
            // a large timeout just to help in case of debugging
            try {
                waitToKill.await(120, TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                System.out.println("Killing server...");
                JMSUtil.crash(liveServer, coreSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    coreSession.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, true);
    Queue queue = sess.createQueue("somequeue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    for (int i = 0; i < 100; i++) {
        TextMessage message = sess.createTextMessage(new String(new byte[10 * 1024]));
        producer.send(message);
        if (i % 10 == 0) {
            sess.commit();
        }
    }
    sess.commit();
    conn.start();
    spoilerThread.start();
    assertTrue(flagAlign.await(10, TimeUnit.SECONDS));
    MessageConsumer consumer = sess.createConsumer(queue);
    // this test is not meant to validate transactionality during Failover as that would require XA and recovery
    for (int i = 0; i < 90; i++) {
        TextMessage message = null;
        int retryNrs = 0;
        do {
            retryNrs++;
            try {
                message = (TextMessage) consumer.receive(5000);
                assertNotNull(message);
                break;
            } catch (JMSException e) {
                new Exception("Exception on receive message", e).printStackTrace();
            }
        } while (retryNrs < 10);
        assertNotNull(message);
        try {
            sess.commit();
        } catch (Exception e) {
            new Exception("Exception during commit", e);
            sess.rollback();
        }
    }
    conn.close();
    spoilerThread.join();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionReceiveContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionReceiveContinuationMessage) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSException(javax.jms.JMSException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) JMSException(javax.jms.JMSException) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 17 with Interceptor

use of org.apache.activemq.artemis.api.core.Interceptor in project wildfly by wildfly.

the class ActiveMQServerService method start.

@Override
public synchronized void start(final StartContext context) throws StartException {
    ClassLoader origTCCL = org.wildfly.security.manager.WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    // Validate whether the AIO native layer can be used
    JournalType jtype = configuration.getJournalType();
    if (jtype == JournalType.ASYNCIO) {
        boolean supportsAIO = AIOSequentialFileFactory.isSupported();
        if (supportsAIO == false) {
            String osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
            if (osName.contains("nux")) {
                ROOT_LOGGER.aioInfoLinux();
            } else {
                ROOT_LOGGER.aioInfo();
            }
            configuration.setJournalType(JournalType.NIO);
        }
    }
    // Setup paths
    configuration.setBindingsDirectory(pathConfig.resolveBindingsPath(pathManager.get()));
    configuration.setLargeMessagesDirectory(pathConfig.resolveLargeMessagePath(pathManager.get()));
    configuration.setJournalDirectory(pathConfig.resolveJournalPath(pathManager.get()));
    configuration.setPagingDirectory(pathConfig.resolvePagingPath(pathManager.get()));
    pathConfig.registerCallbacks(pathManager.get());
    try {
        // Update the acceptor/connector port/host values from the
        // Map the socket bindings onto the connectors/acceptors
        Collection<TransportConfiguration> acceptors = configuration.getAcceptorConfigurations();
        Collection<TransportConfiguration> connectors = configuration.getConnectorConfigurations().values();
        Collection<BroadcastGroupConfiguration> broadcastGroups = configuration.getBroadcastGroupConfigurations();
        Map<String, DiscoveryGroupConfiguration> discoveryGroups = configuration.getDiscoveryGroupConfigurations();
        TransportConfigOperationHandlers.processConnectorBindings(connectors, socketBindings, outboundSocketBindings);
        if (acceptors != null) {
            for (TransportConfiguration tc : acceptors) {
                // If there is a socket binding set the HOST/PORT values
                Object socketRef = tc.getParams().remove(SOCKET_REF);
                if (socketRef != null) {
                    String name = socketRef.toString();
                    SocketBinding binding = socketBindings.get(name).get();
                    if (binding == null) {
                        throw MessagingLogger.ROOT_LOGGER.failedToFindConnectorSocketBinding(tc.getName());
                    }
                    binding.getSocketBindings().getNamedRegistry().registerBinding(ManagedBinding.Factory.createSimpleManagedBinding(binding));
                    InetSocketAddress socketAddress = binding.getSocketAddress();
                    tc.getParams().put(HOST, socketAddress.getAddress().getHostAddress());
                    tc.getParams().put(PORT, socketAddress.getPort());
                }
            }
        }
        if (broadcastGroups != null) {
            final List<BroadcastGroupConfiguration> newConfigs = new ArrayList<>();
            for (final BroadcastGroupConfiguration config : broadcastGroups) {
                final String name = config.getName();
                final String key = "broadcast" + name;
                if (commandDispatcherFactories.containsKey(key)) {
                    BroadcastCommandDispatcherFactory commandDispatcherFactory = commandDispatcherFactories.get(key).get();
                    String clusterName = clusterNames.get(key);
                    newConfigs.add(JGroupsBroadcastGroupAdd.createBroadcastGroupConfiguration(name, config, commandDispatcherFactory, clusterName));
                } else {
                    final Supplier<SocketBinding> bindingSupplier = groupBindings.get(key);
                    if (bindingSupplier == null) {
                        throw MessagingLogger.ROOT_LOGGER.failedToFindBroadcastSocketBinding(name);
                    }
                    final SocketBinding binding = bindingSupplier.get();
                    binding.getSocketBindings().getNamedRegistry().registerBinding(ManagedBinding.Factory.createSimpleManagedBinding(binding));
                    newConfigs.add(SocketBroadcastGroupAdd.createBroadcastGroupConfiguration(name, config, binding));
                }
            }
            configuration.getBroadcastGroupConfigurations().clear();
            configuration.getBroadcastGroupConfigurations().addAll(newConfigs);
        }
        if (discoveryGroups != null) {
            configuration.setDiscoveryGroupConfigurations(new HashMap<>());
            for (final Map.Entry<String, DiscoveryGroupConfiguration> entry : discoveryGroups.entrySet()) {
                final String name = entry.getKey();
                final String key = "discovery" + name;
                final DiscoveryGroupConfiguration config;
                if (commandDispatcherFactories.containsKey(key)) {
                    BroadcastCommandDispatcherFactory commandDispatcherFactory = commandDispatcherFactories.get(key).get();
                    String clusterName = clusterNames.get(key);
                    config = JGroupsDiscoveryGroupAdd.createDiscoveryGroupConfiguration(name, entry.getValue(), commandDispatcherFactory, clusterName);
                } else {
                    final Supplier<SocketBinding> binding = groupBindings.get(key);
                    if (binding == null) {
                        throw MessagingLogger.ROOT_LOGGER.failedToFindDiscoverySocketBinding(name);
                    }
                    config = SocketDiscoveryGroupAdd.createDiscoveryGroupConfiguration(name, entry.getValue(), binding.get());
                    binding.get().getSocketBindings().getNamedRegistry().registerBinding(ManagedBinding.Factory.createSimpleManagedBinding(binding.get()));
                }
                configuration.getDiscoveryGroupConfigurations().put(name, config);
            }
        }
        // security - if an Elytron domain has been defined we delegate security checks to the Elytron based security manager.
        final ActiveMQSecurityManager securityManager;
        if (configuration.isSecurityEnabled()) {
            if (elytronSecurityDomain.isPresent()) {
                securityManager = new ElytronSecurityManager(elytronSecurityDomain.get().get());
            } else {
                securityManager = new WildFlySecurityManager();
            }
        } else {
            securityManager = null;
        }
        // insert possible credential source hold passwords
        setBridgePasswordsFromCredentialSource();
        setClusterPasswordFromCredentialSource();
        if (dataSource.isPresent()) {
            final DataSource ds = dataSource.get().get();
            DatabaseStorageConfiguration dbConfiguration = (DatabaseStorageConfiguration) configuration.getStoreConfiguration();
            dbConfiguration.setDataSource(ds);
            // inject the datasource into the PropertySQLProviderFactory to be able to determine the
            // type of database for the datasource metadata
            PropertySQLProvider.Factory sqlProviderFactory = new PropertySQLProvider.Factory(ds);
            dbConfiguration.setSqlProvider(sqlProviderFactory);
            configuration.setStoreConfiguration(dbConfiguration);
            ROOT_LOGGER.infof("use JDBC store for Artemis server, bindingsTable:%s", dbConfiguration.getBindingsTableName());
        }
        final MBeanServer mbs = mbeanServer.isPresent() ? mbeanServer.get().get() : null;
        // Now start the server
        server = new ActiveMQServerImpl(configuration, mbs, securityManager);
        if (ServerDefinition.CLUSTER_PASSWORD.getDefaultValue().asString().equals(server.getConfiguration().getClusterPassword())) {
            server.getConfiguration().setClusterPassword(java.util.UUID.randomUUID().toString());
        }
        for (Interceptor incomingInterceptor : incomingInterceptors) {
            server.getServiceRegistry().addIncomingInterceptor(incomingInterceptor);
        }
        for (Interceptor outgoingInterceptor : outgoingInterceptors) {
            server.getServiceRegistry().addOutgoingInterceptor(outgoingInterceptor);
        }
    // the server is actually started by the Jakarta Messaging Service.
    } catch (Exception e) {
        throw MessagingLogger.ROOT_LOGGER.failedToStartService(e);
    } finally {
        org.wildfly.security.manager.WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(origTCCL);
    }
}
Also used : OutboundSocketBinding(org.jboss.as.network.OutboundSocketBinding) SocketBinding(org.jboss.as.network.SocketBinding) BroadcastCommandDispatcherFactory(org.wildfly.extension.messaging.activemq.broadcast.BroadcastCommandDispatcherFactory) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) AIOSequentialFileFactory(org.apache.activemq.artemis.core.io.aio.AIOSequentialFileFactory) BroadcastCommandDispatcherFactory(org.wildfly.extension.messaging.activemq.broadcast.BroadcastCommandDispatcherFactory) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) BroadcastGroupConfiguration(org.apache.activemq.artemis.api.core.BroadcastGroupConfiguration) ActiveMQSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) MBeanServer(javax.management.MBeanServer) PropertySQLProvider(org.apache.activemq.artemis.jdbc.store.sql.PropertySQLProvider) DiscoveryGroupConfiguration(org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) StartException(org.jboss.msc.service.StartException) DataSource(javax.sql.DataSource) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) JournalType(org.apache.activemq.artemis.core.server.JournalType) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Interceptor (org.apache.activemq.artemis.api.core.Interceptor)17 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)11 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)8 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)7 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)6 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)4 ArrayList (java.util.ArrayList)3 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)2 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)2