@Override protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException { boolean isForcedDurable = NetworkBridgeUtils.isForcedDurable(info, dynamicallyIncludedDestinations, staticallyIncludedDestinations); if (addToAlreadyInterestedConsumers(info, isForcedDurable)) { return null; // don't want this subscription added } //add our original id to ourselves info.addNetworkConsumerId(info.getConsumerId()); ConsumerId forcedDurableId = isForcedDurable ? info.getConsumerId() : null; if(info.isDurable() || isForcedDurable) { // set the subscriber name to something reproducible info.setSubscriptionName(getSubscriberName(info.getDestination())); // and override the consumerId with something unique so that it won't // be removed if the durable subscriber (at the other end) goes away info.setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId())); } info.setSelector(null); DemandSubscription demandSubscription = doCreateDemandSubscription(info); if (forcedDurableId != null) { demandSubscription.addForcedDurableConsumer(forcedDurableId); forcedDurableRemoteId.add(forcedDurableId); } return demandSubscription; }
@Override public long getLastMessageBrokerSequenceId() throws IOException { TransactionContext c = getTransactionContext(); try { long seq = getAdapter().doGetLastMessageStoreSequenceId(c); sequenceGenerator.setLastSequenceId(seq); long brokerSeq = 0; if (seq != 0) { byte[] msg = getAdapter().doGetMessageById(c, seq); if (msg != null) { Message last = (Message)wireFormat.unmarshal(new ByteSequence(msg)); brokerSeq = last.getMessageId().getBrokerSequenceId(); } else { LOG.warn("Broker sequence id wasn't recovered properly, possible duplicates!"); } } return brokerSeq; } catch (SQLException e) { JDBCPersistenceAdapter.log("JDBC Failure: ", e); throw IOExceptionSupport.create("Failed to get last broker message id: " + e, e); } finally { c.close(); } }
private long minPendingSequeunceId() { synchronized (pendingAdditions) { if (!pendingAdditions.isEmpty()) { return pendingAdditions.get(0); } else { // nothing pending, ensure scan is limited to current state return persistenceAdapter.sequenceGenerator.getLastSequenceId() + 1; } } }
protected DemandSubscription doCreateDemandSubscription(ConsumerInfo info) throws IOException { DemandSubscription result = new DemandSubscription(info); result.getLocalInfo().setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId())); if (info.getDestination().isTemporary()) { // reset the local connection Id ActiveMQTempDestination dest = (ActiveMQTempDestination) result.getLocalInfo().getDestination(); dest.setConnectionId(localConnectionInfo.getConnectionId().toString()); } if (configuration.isDecreaseNetworkConsumerPriority()) { byte priority = (byte) configuration.getConsumerPriorityBase(); if (info.getBrokerPath() != null && info.getBrokerPath().length > 1) { // The longer the path to the consumer, the less it's consumer priority. priority -= info.getBrokerPath().length + 1; } result.getLocalInfo().setPriority(priority); LOG.debug("{} using priority: {} for subscription: {}", new Object[]{configuration.getBrokerName(), priority, info}); } configureDemandSubscription(info, result); return result; }
final protected DemandSubscription createDemandSubscription(ActiveMQDestination destination, final String subscriptionName) { ConsumerInfo info = new ConsumerInfo(); info.setNetworkSubscription(true); info.setDestination(destination); if (subscriptionName != null) { info.setSubscriptionName(subscriptionName); } // Indicate that this subscription is being made on behalf of the remote broker. info.setBrokerPath(new BrokerId[]{remoteBrokerId}); // the remote info held by the DemandSubscription holds the original // consumerId, the local info get's overwritten info.setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId())); DemandSubscription result = null; try { result = createDemandSubscription(info); } catch (IOException e) { LOG.error("Failed to create DemandSubscription ", e); } return result; }
SessionId sessionId = new SessionId(info.getConnectionId(), -1); ConsumerInfo info = new ConsumerInfo(new ConsumerId(sessionId, consumerIdGenerator.getNextSequenceId())); info.setDestination(ActiveMQMessageTransformation.transformDestination(topic)); info.setSubscriptionName(subscriptionName); info.setSelector(messageSelector); info.setPrefetchSize(maxMessages);
protected void sendStats(ConnectionContext context, ActiveMQMapMessage msg, ActiveMQDestination replyTo) throws Exception { msg.setPersistent(false); msg.setTimestamp(System.currentTimeMillis()); msg.setPriority((byte) javax.jms.Message.DEFAULT_PRIORITY); msg.setType(AdvisorySupport.ADIVSORY_MESSAGE_TYPE); msg.setMessageId(new MessageId(this.advisoryProducerId, this.messageIdGenerator.getNextSequenceId())); msg.setDestination(replyTo); msg.setResponseRequired(false); msg.setProducerId(this.advisoryProducerId); boolean originalFlowControl = context.isProducerFlowControl(); final ProducerBrokerExchange producerExchange = new ProducerBrokerExchange(); producerExchange.setConnectionContext(context); producerExchange.setMutable(true); producerExchange.setProducerState(new ProducerState(new ProducerInfo())); try { context.setProducerFlowControl(false); this.next.send(producerExchange, msg); } finally { context.setProducerFlowControl(originalFlowControl); } }
/** * Send the ConnectionInfo to the Broker * * @throws JMSException */ protected void ensureConnectionInfoSent() throws JMSException { synchronized(this.ensureConnectionInfoSentMutex) { // Can we skip sending the ConnectionInfo packet?? if (isConnectionInfoSentToBroker || closed.get()) { return; } //TODO shouldn't this check be on userSpecifiedClientID rather than the value of clientID? if (info.getClientId() == null || info.getClientId().trim().length() == 0) { info.setClientId(clientIdGenerator.generateId()); } syncSendPacket(info.copy(), getConnectResponseTimeout()); this.isConnectionInfoSentToBroker = true; // Add a temp destination advisory consumer so that // We know what the valid temporary destinations are on the // broker without having to do an RPC to the broker. ConsumerId consumerId = new ConsumerId(new SessionId(info.getConnectionId(), -1), consumerIdGenerator.getNextSequenceId()); if (watchTopicAdvisories) { advisoryConsumer = new AdvisoryConsumer(this, consumerId); } } }
if (brokerConsumerDests.get(pair) == null) { ConnectionId connectionId = new ConnectionId(connectionIdGenerator.generateId()); SessionId sessionId = new SessionId(connectionId, sessionIdGenerator.getNextSequenceId()); ConsumerId consumerId = new ConsumerId(sessionId, consumerIdGenerator.getNextSequenceId()); info = new ConsumerInfo(consumerId); ActiveMQTopic topic = AdvisorySupport.getVirtualDestinationConsumerAdvisoryTopic(info.getDestination()); fireConsumerAdvisory(context, info.getDestination(), topic, info);
/** * @return sessionId */ protected SessionId getNextSessionId() { return new SessionId(info.getConnectionId(), sessionIdGenerator.getNextSequenceId()); }
ConsumerId id = new ConsumerId(sessionId, consumerIdGenerator.getNextSequenceId()); ConsumerInfo consumerInfo = new ConsumerInfo(id); consumerInfo.setPrefetchSize(1000); consumerInfo.setDispatchAsync(true);
final protected DemandSubscription createDemandSubscription(ActiveMQDestination destination) { ConsumerInfo info = new ConsumerInfo(); info.setDestination(destination); // the remote info held by the DemandSubscription holds the original // consumerId, // the local info get's overwritten info.setConsumerId(new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId())); DemandSubscription result = null; try { result = createDemandSubscription(info); } catch (IOException e) { LOG.error("Failed to create DemandSubscription ", e); } if (result != null) { result.getLocalInfo().setPriority(ConsumerInfo.NETWORK_CONSUMER_PRIORITY); } return result; }
/** * @return a newly created ConsumedId unique to this connection session instance. */ private ConsumerId createConsumerId() { return new ConsumerId(connectionSessionId, consumerIdGenerator.getNextSequenceId()); }
/** * @return a unique ConsumerId instance. */ protected ConsumerId getNextConsumerId() { return new ConsumerId(info.getSessionId(), consumerIdGenerator.getNextSequenceId()); }
ActiveMQMessage convertMessage(PUBLISH command) throws JMSException { ActiveMQBytesMessage msg = new ActiveMQBytesMessage(); msg.setProducerId(producerId); MessageId id = new MessageId(producerId, publisherIdGenerator.getNextSequenceId()); msg.setMessageId(id); LOG.trace("MQTT-->ActiveMQ: MQTT_MSGID:{} client:{} connection:{} ActiveMQ_MSGID:{}", command.messageId(), clientId, connectionInfo.getConnectionId(), msg.getMessageId()); msg.setTimestamp(System.currentTimeMillis()); msg.setPriority((byte) Message.DEFAULT_PRIORITY); msg.setPersistent(command.qos() != QoS.AT_MOST_ONCE && !command.retain()); msg.setIntProperty(QOS_PROPERTY_NAME, command.qos().ordinal()); if (command.retain()) { msg.setBooleanProperty(RETAIN_PROPERTY, true); } ActiveMQDestination destination; synchronized (activeMQDestinationMap) { destination = activeMQDestinationMap.get(command.topicName().toString()); if (destination == null) { String topicName = MQTTProtocolSupport.convertMQTTToActiveMQ(command.topicName().toString()); destination = new ActiveMQTopic(topicName); activeMQDestinationMap.put(command.topicName().toString(), destination); } } msg.setJMSDestination(destination); msg.writeBytes(command.payload().data, command.payload().offset, command.payload().length); return msg; }
private void doSchedule(Message messageSend, Object cronValue, Object periodValue, Object delayValue) throws Exception { long delay = 0; long period = 0; int repeat = 0; String cronEntry = ""; // clear transaction context Message msg = messageSend.copy(); msg.setTransactionId(null); org.apache.activemq.util.ByteSequence packet = wireFormat.marshal(msg); if (cronValue != null) { cronEntry = cronValue.toString(); } if (periodValue != null) { period = (Long) TypeConversionSupport.convert(periodValue, Long.class); } if (delayValue != null) { delay = (Long) TypeConversionSupport.convert(delayValue, Long.class); } Object repeatValue = msg.getProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT); if (repeatValue != null) { repeat = (Integer) TypeConversionSupport.convert(repeatValue, Integer.class); } //job id should be unique for every job (Same format as MessageId) MessageId jobId = new MessageId(messageSend.getMessageId().getProducerId(), longGenerator.getNextSequenceId()); getInternalScheduler().schedule(jobId.toString(), new ByteSequence(packet.data, packet.offset, packet.length), cronEntry, delay, period, repeat); }
protected void onStompSend(StompFrame command) throws IOException, JMSException { checkConnected(); Map<String, String> headers = command.getHeaders(); String stompTx = headers.get(Stomp.Headers.TRANSACTION); headers.remove("transaction"); ActiveMQMessage message = convertMessage(command); message.setProducerId(producerId); MessageId id = new MessageId(producerId, messageIdGenerator.getNextSequenceId()); message.setMessageId(id); message.setJMSTimestamp(System.currentTimeMillis()); if (stompTx != null) { TransactionId activemqTx = transactions.get(stompTx); if (activemqTx == null) { throw new ProtocolException("Invalid transaction id: " + stompTx); } message.setTransactionId(activemqTx); } message.onSend(); sendToActiveMQ(message, createResponseHandler(command)); }
/** * @return a unique ProducerId instance. */ protected ProducerId getNextProducerId() { return new ProducerId(info.getSessionId(), producerIdGenerator.getNextSequenceId()); }
/** * @return the broker sequence id */ @Override public long getBrokerSequenceId() { synchronized (sequenceGenerator) { return sequenceGenerator.getNextSequenceId(); } }
public RegionBroker(BrokerService brokerService, TaskRunnerFactory taskRunnerFactory, SystemUsage memoryManager, DestinationFactory destinationFactory, DestinationInterceptor destinationInterceptor, Scheduler scheduler, ThreadPoolExecutor executor) throws IOException { this.brokerService = brokerService; this.executor = executor; this.scheduler = scheduler; if (destinationFactory == null) { throw new IllegalArgumentException("null destinationFactory"); } this.sequenceGenerator.setLastSequenceId(destinationFactory.getLastMessageBrokerSequenceId()); this.destinationFactory = destinationFactory; queueRegion = createQueueRegion(memoryManager, taskRunnerFactory, destinationFactory); topicRegion = createTopicRegion(memoryManager, taskRunnerFactory, destinationFactory); this.destinationInterceptor = destinationInterceptor; tempQueueRegion = createTempQueueRegion(memoryManager, taskRunnerFactory, destinationFactory); tempTopicRegion = createTempTopicRegion(memoryManager, taskRunnerFactory, destinationFactory); this.taskRunnerFactory = taskRunnerFactory; }