use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.
the class MQTTSubscriptionManager method createQueueForSubscription.
/**
* Creates a Queue if it doesn't already exist, based on a topic and address. Returning the queue name.
*/
private Queue createQueueForSubscription(String address, int qos) throws Exception {
// Check to see if a subscription queue already exists.
SimpleString queue = getQueueNameForTopic(address);
Queue q = session.getServer().locateQueue(queue);
// The queue does not exist so we need to create it.
if (q == null) {
SimpleString sAddress = SimpleString.toSimpleString(address);
// Check we can auto create queues.
BindingQueryResult bindingQueryResult = session.getServerSession().executeBindingQuery(sAddress);
if (!bindingQueryResult.isAutoCreateQueues()) {
throw ActiveMQMessageBundle.BUNDLE.noSuchQueue(sAddress);
}
// Check that the address exists, if not we try to auto create it.
AddressInfo addressInfo = session.getServerSession().getAddress(sAddress);
if (addressInfo == null) {
if (!bindingQueryResult.isAutoCreateAddresses()) {
throw ActiveMQMessageBundle.BUNDLE.addressDoesNotExist(SimpleString.toSimpleString(address));
}
addressInfo = session.getServerSession().createAddress(SimpleString.toSimpleString(address), RoutingType.MULTICAST, true);
}
return findOrCreateQueue(bindingQueryResult, addressInfo, queue, qos);
}
return q;
}
use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.
the class OpenWireConnection method addDestination.
public void addDestination(DestinationInfo info) throws Exception {
boolean created = false;
ActiveMQDestination dest = info.getDestination();
if (!protocolManager.isSupportAdvisory() && AdvisorySupport.isAdvisoryTopic(dest)) {
return;
}
SimpleString qName = SimpleString.toSimpleString(dest.getPhysicalName());
if (server.locateQueue(qName) == null) {
AddressSettings addressSettings = server.getAddressSettingsRepository().getMatch(dest.getPhysicalName());
AddressInfo addressInfo = new AddressInfo(qName, dest.isTopic() ? RoutingType.MULTICAST : RoutingType.ANYCAST);
if (AdvisorySupport.isAdvisoryTopic(dest) && protocolManager.isSuppressInternalManagementObjects()) {
addressInfo.setInternal(true);
}
if (dest.isQueue() && (addressSettings.isAutoCreateQueues() || dest.isTemporary())) {
try {
internalSession.createQueue(addressInfo, qName, null, dest.isTemporary(), !dest.isTemporary(), !dest.isTemporary());
created = true;
} catch (ActiveMQQueueExistsException exists) {
// The queue may have been created by another thread in the mean time. Catch and do nothing.
}
} else if (dest.isTopic() && (addressSettings.isAutoCreateAddresses() || dest.isTemporary())) {
try {
internalSession.createAddress(addressInfo, !dest.isTemporary());
created = true;
} catch (ActiveMQAddressExistsException exists) {
// The address may have been created by another thread in the mean time. Catch and do nothing.
}
}
}
if (dest.isTemporary()) {
// Openwire needs to store the DestinationInfo in order to send
// Advisory messages to clients
this.state.addTempDestination(info);
}
if (created && !AdvisorySupport.isAdvisoryTopic(dest)) {
AMQConnectionContext context = getContext();
DestinationInfo advInfo = new DestinationInfo(context.getConnectionId(), DestinationInfo.ADD_OPERATION_TYPE, dest);
ActiveMQTopic topic = AdvisorySupport.getDestinationAdvisoryTopic(dest);
protocolManager.fireAdvisory(context, topic, advInfo);
}
}
use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.
the class AMQConsumer method createTopicSubscription.
private SimpleString createTopicSubscription(boolean isDurable, String clientID, String physicalName, String subscriptionName, SimpleString selector, SimpleString address) throws Exception {
SimpleString queueName;
AddressInfo addressInfo = session.getCoreServer().getAddressInfo(address);
if (addressInfo != null) {
addressInfo.addRoutingType(RoutingType.MULTICAST);
} else {
addressInfo = new AddressInfo(address, RoutingType.MULTICAST);
}
addressInfo.setInternal(internalAddress);
if (isDurable) {
queueName = org.apache.activemq.artemis.jms.client.ActiveMQDestination.createQueueNameForSubscription(true, clientID, subscriptionName);
QueueQueryResult result = session.getCoreSession().executeQueueQuery(queueName);
if (result.isExists()) {
// Already exists
if (result.getConsumerCount() > 0) {
throw new IllegalStateException("Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
}
SimpleString oldFilterString = result.getFilterString();
boolean selectorChanged = selector == null && oldFilterString != null || oldFilterString == null && selector != null || oldFilterString != null && selector != null && !oldFilterString.equals(selector);
SimpleString oldTopicName = result.getAddress();
boolean topicChanged = !oldTopicName.equals(address);
if (selectorChanged || topicChanged) {
// Delete the old durable sub
session.getCoreSession().deleteQueue(queueName);
// Create the new one
session.getCoreSession().createQueue(addressInfo, queueName, selector, false, true);
}
} else {
session.getCoreSession().createQueue(addressInfo, queueName, selector, false, true);
}
} else {
queueName = new SimpleString(UUID.randomUUID().toString());
session.getCoreSession().createQueue(addressInfo, queueName, selector, true, false);
}
return queueName;
}
use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.
the class FindDestinationTest method testFindTopic.
@Test
public void testFindTopic() throws Exception {
server.getActiveMQServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("testTopic"), RoutingType.MULTICAST));
server.getActiveMQServer().createQueue(new SimpleString("testTopic"), RoutingType.MULTICAST, new SimpleString("testTopic"), null, false, false);
ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/topics/testTopic"));
ClientResponse<?> response = request.head();
response.releaseConnection();
Assert.assertEquals(200, response.getStatus());
Link sender = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "create");
Link subscriptions = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), response, "pull-subscriptions");
ClientResponse<?> res = subscriptions.request().post();
Assert.assertEquals(201, res.getStatus());
Link sub1 = res.getLocationLink();
res.releaseConnection();
Assert.assertNotNull(sub1);
Link consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
Assert.assertNotNull(consumeNext1);
System.out.println("consumeNext1: " + consumeNext1);
res = subscriptions.request().post();
Assert.assertEquals(201, res.getStatus());
Link sub2 = res.getLocationLink();
res.releaseConnection();
Assert.assertNotNull(sub2);
Link consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
Assert.assertNotNull(consumeNext1);
res = sender.request().body("text/plain", Integer.toString(1)).post();
res.releaseConnection();
Assert.assertEquals(201, res.getStatus());
res = sender.request().body("text/plain", Integer.toString(2)).post();
res.releaseConnection();
Assert.assertEquals(201, res.getStatus());
res = consumeNext1.request().post(String.class);
Assert.assertEquals(200, res.getStatus());
Assert.assertEquals("1", res.getEntity(String.class));
res.releaseConnection();
consumeNext1 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
res = consumeNext1.request().post(String.class);
Assert.assertEquals(200, res.getStatus());
Assert.assertEquals("2", res.getEntity(String.class));
res.releaseConnection();
res = consumeNext2.request().post(String.class);
Assert.assertEquals(200, res.getStatus());
Assert.assertEquals("1", res.getEntity(String.class));
res.releaseConnection();
consumeNext2 = getLinkByTitle(manager.getTopicManager().getLinkStrategy(), res, "consume-next");
res = consumeNext2.request().post(String.class);
Assert.assertEquals(200, res.getStatus());
Assert.assertEquals("2", res.getEntity(String.class));
res.releaseConnection();
res = sub1.request().delete();
res.releaseConnection();
Assert.assertEquals(204, res.getStatus());
res = sub2.request().delete();
res.releaseConnection();
Assert.assertEquals(204, res.getStatus());
}
use of org.apache.activemq.artemis.core.server.impl.AddressInfo in project activemq-artemis by apache.
the class JMSServerManagerImpl method internalCreateTopic.
private synchronized boolean internalCreateTopic(final String address, final String topicName, final boolean autoCreated) throws Exception {
if (topics.get(address) != null) {
return false;
} else {
// Create the JMS topic with topicName as the logical name of the topic *and* address as its address
ActiveMQTopic activeMQTopic = ActiveMQDestination.createTopic(address, topicName);
server.addOrUpdateAddressInfo(new AddressInfo(SimpleString.toSimpleString(activeMQTopic.getAddress()), RoutingType.MULTICAST));
topics.put(address, activeMQTopic);
this.recoverregistryBindings(topicName, PersistedType.Topic);
return true;
}
}
Aggregations