Search in sources :

Example 26 with Binding

use of org.apache.activemq.artemis.core.postoffice.Binding in project activemq-artemis by apache.

the class BMFailoverTest method testFailoverOnCommit2.

@Test
@BMRules(rules = { @BMRule(name = "trace clientsessionimpl commit", targetClass = "org.apache.activemq.artemis.core.client.impl.ClientSessionImpl", targetMethod = "start(javax.transaction.xa.Xid, int)", targetLocation = "AT EXIT", action = "org.apache.activemq.artemis.tests.extras.byteman.BMFailoverTest.serverToStop.getServer().stop(true)") })
public void testFailoverOnCommit2() throws Exception {
    serverToStop = liveServer;
    locator = getServerLocator().setFailoverOnInitialConnection(true);
    SimpleString inQueue = new SimpleString("inQueue");
    SimpleString outQueue = new SimpleString("outQueue");
    createSessionFactory();
    createSessionFactory2();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        session.createQueue(inQueue, inQueue, null, true);
        session.createQueue(outQueue, outQueue, null, true);
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    ClientSession xaSessionRec = addClientSession(sf.createSession(true, false, false));
    ClientConsumer consumer = addClientConsumer(xaSessionRec.createConsumer(inQueue));
    byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    Xid xidRec = new XidImpl("xa2".getBytes(), 1, globalTransactionId);
    xaSessionRec.start();
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    // failover is now occurring, receive, ack and end will be called whilst this is happening.
    ClientMessageImpl m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    System.out.println("********************" + m.getIntProperty("counter"));
    // the mdb would ack the message before calling onMessage()
    m.acknowledge();
    try {
        // this may fail but thats ok, it depends on the race and when failover actually happens
        xaSessionRec.end(xidRec, XAResource.TMSUCCESS);
    } catch (XAException ignore) {
    }
    // we always reset the client on the RA
    ((ClientSessionInternal) xaSessionRec).resetIfNeeded();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    // now receive and send a message successfully
    globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    xidRec = new XidImpl("xa4".getBytes(), 1, globalTransactionId);
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    Binding binding = backupServer.getServer().getPostOffice().getBinding(inQueue);
    Queue inQ = (Queue) binding.getBindable();
    m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    // the mdb would ack the message before calling onMessage()
    m.acknowledge();
    System.out.println("********************" + m.getIntProperty("counter"));
    xaSessionRec.getXAResource().end(xidRec, XAResource.TMSUCCESS);
    xaSessionRec.getXAResource().prepare(xidRec);
    xaSessionRec.getXAResource().commit(xidRec, false);
    // let's close the consumer so anything pending is handled
    consumer.close();
    assertEquals(1, getMessageCount(inQ));
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 27 with Binding

use of org.apache.activemq.artemis.core.postoffice.Binding in project activemq-artemis by apache.

the class DivertTest method testInjectedTransformer.

@Test
public void testInjectedTransformer() throws Exception {
    final SimpleString ADDRESS = new SimpleString("myAddress");
    final String DIVERT = "myDivert";
    ServiceRegistryImpl serviceRegistry = new ServiceRegistryImpl();
    Transformer transformer = new Transformer() {

        @Override
        public Message transform(Message message) {
            return null;
        }
    };
    serviceRegistry.addDivertTransformer(DIVERT, transformer);
    ActiveMQServer server = addServer(new ActiveMQServerImpl(null, null, null, null, serviceRegistry));
    server.start();
    server.waitForActivation(100, TimeUnit.MILLISECONDS);
    server.createQueue(ADDRESS, RoutingType.MULTICAST, SimpleString.toSimpleString("myQueue"), null, false, false);
    server.deployDivert(new DivertConfiguration().setName(DIVERT).setAddress(ADDRESS.toString()).setForwardingAddress(ADDRESS.toString()));
    Collection<Binding> bindings = server.getPostOffice().getBindingsForAddress(ADDRESS).getBindings();
    Divert divert = null;
    for (Binding binding : bindings) {
        if (binding instanceof DivertBinding) {
            divert = ((DivertBinding) binding).getDivert();
        }
    }
    assertNotNull(divert);
    assertEquals(transformer, divert.getTransformer());
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) DivertBinding(org.apache.activemq.artemis.core.postoffice.impl.DivertBinding) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ServiceRegistryImpl(org.apache.activemq.artemis.core.server.impl.ServiceRegistryImpl) Transformer(org.apache.activemq.artemis.core.server.transformer.Transformer) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) DivertBinding(org.apache.activemq.artemis.core.postoffice.impl.DivertBinding) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) Divert(org.apache.activemq.artemis.core.server.Divert) Test(org.junit.Test)

Example 28 with Binding

use of org.apache.activemq.artemis.core.postoffice.Binding in project activemq-artemis by apache.

the class PostOfficeJournalLoader method initQueues.

@Override
public void initQueues(Map<Long, QueueBindingInfo> queueBindingInfosMap, List<QueueBindingInfo> queueBindingInfos) throws Exception {
    int duplicateID = 0;
    for (final QueueBindingInfo queueBindingInfo : queueBindingInfos) {
        queueBindingInfosMap.put(queueBindingInfo.getId(), queueBindingInfo);
        final Filter filter = FilterImpl.createFilter(queueBindingInfo.getFilterString());
        final boolean isTopicIdentification = FilterUtils.isTopicIdentification(filter);
        if (postOffice.getBinding(queueBindingInfo.getQueueName()) != null) {
            if (isTopicIdentification) {
                final long tx = storageManager.generateID();
                storageManager.deleteQueueBinding(tx, queueBindingInfo.getId());
                storageManager.commitBindings(tx);
                continue;
            } else {
                final SimpleString newName = queueBindingInfo.getQueueName().concat("-" + (duplicateID++));
                ActiveMQServerLogger.LOGGER.queueDuplicatedRenaming(queueBindingInfo.getQueueName().toString(), newName.toString());
                queueBindingInfo.replaceQueueName(newName);
            }
        }
        final QueueConfig.Builder queueConfigBuilder;
        if (queueBindingInfo.getAddress() == null) {
            queueConfigBuilder = QueueConfig.builderWith(queueBindingInfo.getId(), queueBindingInfo.getQueueName());
        } else {
            queueConfigBuilder = QueueConfig.builderWith(queueBindingInfo.getId(), queueBindingInfo.getQueueName(), queueBindingInfo.getAddress());
        }
        queueConfigBuilder.filter(filter).pagingManager(pagingManager).user(queueBindingInfo.getUser()).durable(true).temporary(false).autoCreated(queueBindingInfo.isAutoCreated()).purgeOnNoConsumers(queueBindingInfo.isPurgeOnNoConsumers()).maxConsumers(queueBindingInfo.getMaxConsumers()).exclusive(queueBindingInfo.isExclusive()).lastValue(queueBindingInfo.isLastValue()).routingType(RoutingType.getType(queueBindingInfo.getRoutingType()));
        final Queue queue = queueFactory.createQueueWith(queueConfigBuilder.build());
        queue.setConsumersRefCount(new QueueManagerImpl(((PostOfficeImpl) postOffice).getServer(), queueBindingInfo.getQueueName()));
        if (queueBindingInfo.getQueueStatusEncodings() != null) {
            for (QueueStatusEncoding encoding : queueBindingInfo.getQueueStatusEncodings()) {
                if (encoding.getStatus() == QueueStatus.PAUSED)
                    queue.reloadPause(encoding.getId());
            }
        }
        final Binding binding = new LocalQueueBinding(queue.getAddress(), queue, nodeManager.getNodeId());
        queues.put(queue.getID(), queue);
        postOffice.addBinding(binding);
        managementService.registerQueue(queue, queue.getAddress(), storageManager);
    }
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) GroupBinding(org.apache.activemq.artemis.core.server.group.impl.GroupBinding) QueueConfig(org.apache.activemq.artemis.core.server.QueueConfig) QueueStatusEncoding(org.apache.activemq.artemis.core.persistence.impl.journal.codec.QueueStatusEncoding) QueueBindingInfo(org.apache.activemq.artemis.core.persistence.QueueBindingInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) Filter(org.apache.activemq.artemis.core.filter.Filter) Queue(org.apache.activemq.artemis.core.server.Queue) PostOfficeImpl(org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl)

Example 29 with Binding

use of org.apache.activemq.artemis.core.postoffice.Binding in project activemq-artemis by apache.

the class QueueImpl method locateTargetBinding.

private Pair<String, Binding> locateTargetBinding(SimpleString queueSuffix, Message copyMessage, long oldQueueID) {
    String targetNodeID = null;
    Binding targetBinding = null;
    for (Map.Entry<SimpleString, Binding> entry : postOffice.getAllBindings().entrySet()) {
        Binding binding = entry.getValue();
        // we only care about the remote queue bindings
        if (binding instanceof RemoteQueueBinding) {
            RemoteQueueBinding remoteQueueBinding = (RemoteQueueBinding) binding;
            // does this remote queue binding point to the same queue as the message?
            if (oldQueueID == remoteQueueBinding.getRemoteQueueID()) {
                // get the name of this queue so we can find the corresponding remote queue binding pointing to the scale down target node
                SimpleString oldQueueName = remoteQueueBinding.getRoutingName();
                // parse the queue name of the remote queue binding to determine the node ID
                String temp = remoteQueueBinding.getQueue().getName().toString();
                targetNodeID = temp.substring(temp.lastIndexOf(".") + 1);
                logger.debug("Message formerly destined for " + oldQueueName + " with ID: " + oldQueueID + " on address " + copyMessage.getAddressSimpleString() + " on node " + targetNodeID);
                // now that we have the name of the queue we need to look through all the bindings again to find the new remote queue binding
                for (Map.Entry<SimpleString, Binding> entry2 : postOffice.getAllBindings().entrySet()) {
                    binding = entry2.getValue();
                    // again, we only care about the remote queue bindings
                    if (binding instanceof RemoteQueueBinding) {
                        remoteQueueBinding = (RemoteQueueBinding) binding;
                        temp = remoteQueueBinding.getQueue().getName().toString();
                        targetNodeID = temp.substring(temp.lastIndexOf(".") + 1);
                        if (oldQueueName.equals(remoteQueueBinding.getRoutingName()) && targetNodeID.equals(queueSuffix.toString())) {
                            targetBinding = remoteQueueBinding;
                            if (logger.isDebugEnabled()) {
                                logger.debug("Message now destined for " + remoteQueueBinding.getRoutingName() + " with ID: " + remoteQueueBinding.getRemoteQueueID() + " on address " + copyMessage.getAddress() + " on node " + targetNodeID);
                            }
                            break;
                        } else {
                            logger.debug("Failed to match: " + remoteQueueBinding);
                        }
                    }
                }
            }
        }
    }
    return new Pair<>(targetNodeID, targetBinding);
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) RemoteQueueBinding(org.apache.activemq.artemis.core.server.cluster.RemoteQueueBinding) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Map(java.util.Map) HashMap(java.util.HashMap) RemoteQueueBinding(org.apache.activemq.artemis.core.server.cluster.RemoteQueueBinding) Pair(org.apache.activemq.artemis.api.core.Pair)

Example 30 with Binding

use of org.apache.activemq.artemis.core.postoffice.Binding in project activemq-artemis by apache.

the class ScaleDownHandler method scaleDownMessages.

public long scaleDownMessages(ClientSessionFactory sessionFactory, SimpleString nodeId, String user, String password) throws Exception {
    long messageCount = 0;
    targetNodeId = nodeId != null ? nodeId.toString() : getTargetNodeId(sessionFactory);
    try (ClientSession session = sessionFactory.createSession(user, password, false, true, true, false, 0)) {
        ClientProducer producer = session.createProducer();
        // perform a loop per address
        for (SimpleString address : postOffice.getAddresses()) {
            logger.debug("Scaling down address " + address);
            Bindings bindings = postOffice.getBindingsForAddress(address);
            // It will get a list of queues on this address, ordered by the number of messages
            Set<Queue> queues = new TreeSet<>(new OrderQueueByNumberOfReferencesComparator());
            for (Binding binding : bindings.getBindings()) {
                if (binding instanceof LocalQueueBinding) {
                    Queue queue = ((LocalQueueBinding) binding).getQueue();
                    // as part of scale down we will cancel any scheduled message and pass it to theWhile we scan for the queues we will also cancel any scheduled messages and deliver them right away
                    queue.deliverScheduledMessages();
                    queues.add(queue);
                }
            }
            String sfPrefix = ((PostOfficeImpl) postOffice).getServer().getInternalNamingPrefix() + "sf.";
            if (address.toString().startsWith(sfPrefix)) {
                messageCount += scaleDownSNF(address, queues, producer);
            } else {
                messageCount += scaleDownRegularMessages(address, queues, session, producer);
            }
        }
    }
    return messageCount;
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) TreeSet(java.util.TreeSet) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) PostOfficeImpl(org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl)

Aggregations

Binding (org.apache.activemq.artemis.core.postoffice.Binding)81 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)52 LocalQueueBinding (org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding)29 QueueBinding (org.apache.activemq.artemis.core.postoffice.QueueBinding)28 Test (org.junit.Test)25 Bindings (org.apache.activemq.artemis.core.postoffice.Bindings)24 Queue (org.apache.activemq.artemis.core.server.Queue)24 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)18 RemoteQueueBinding (org.apache.activemq.artemis.core.server.cluster.RemoteQueueBinding)17 ArrayList (java.util.ArrayList)12 Filter (org.apache.activemq.artemis.core.filter.Filter)10 DivertBinding (org.apache.activemq.artemis.core.postoffice.impl.DivertBinding)10 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)9 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)9 Map (java.util.Map)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 QueueQueryResult (org.apache.activemq.artemis.core.server.QueueQueryResult)8 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)7 PostOffice (org.apache.activemq.artemis.core.postoffice.PostOffice)7 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)7