Skip to content

Commit

Permalink
Merge pull request #108 from Azure/dev
Browse files Browse the repository at this point in the history
Making a semantic change to queue client, subscription client setPref…
  • Loading branch information
yvgopal authored Aug 11, 2017
2 parents 2095a01 + fb16b69 commit 2b8b400
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MessageAndSessionPump extends InitializableEntity implements IMessageAndSe
private static final Duration MINIMUM_MESSAGE_LOCK_VALIDITY = Duration.ofSeconds(4);
private static final Duration MAXIMUM_RENEW_LOCK_BUFFER = Duration.ofSeconds(10);
private static final Duration SLEEP_DURATION_ON_ACCEPT_SESSION_EXCEPTION = Duration.ofMinutes(1);
private static final int UNSET_PREFETCH_COUNT = -1; // Means prefetch count not set

private final ConcurrentHashMap<String, IMessageSession> openSessions;
private final MessagingFactory factory;
Expand All @@ -44,13 +45,15 @@ class MessageAndSessionPump extends InitializableEntity implements IMessageAndSe
private ISessionHandler sessionHandler;
private MessageHandlerOptions messageHandlerOptions;
private SessionHandlerOptions sessionHandlerOptions;
private int prefetchCount;

public MessageAndSessionPump(MessagingFactory factory, String entityPath, ReceiveMode receiveMode) {
super(StringUtil.getShortRandomString(), null);
this.factory = factory;
this.entityPath = entityPath;
this.receiveMode = receiveMode;
this.openSessions = new ConcurrentHashMap<>();
this.prefetchCount = UNSET_PREFETCH_COUNT;
}

@Override
Expand All @@ -67,6 +70,10 @@ public void registerMessageHandler(IMessageHandler handler, MessageHandlerOption

this.innerReceiver = ClientFactory.createMessageReceiverFromEntityPath(this.factory, this.entityPath, this.receiveMode);
TRACE_LOGGER.info("Created MessageReceiver to entity '{}'", this.entityPath);
if(this.prefetchCount != UNSET_PREFETCH_COUNT)
{
this.innerReceiver.setPrefetchCount(this.prefetchCount);
}
for (int i = 0; i < handlerOptions.getMaxConcurrentCalls(); i++) {
this.receiveAndPumpMessage();
}
Expand Down Expand Up @@ -221,6 +228,14 @@ private void acceptSessionsAndPumpMessage() {
} else {
// Received a session.. Now pump messages..
TRACE_LOGGER.debug("Accepted a session '{}' from entity '{}'", session.getSessionId(), this.entityPath);
if(this.prefetchCount != UNSET_PREFETCH_COUNT)
{
try {
session.setPrefetchCount(this.prefetchCount);
} catch (ServiceBusException e) {
// Should not happen as long as reactor is running. So ignoring
}
}
this.openSessions.put(session.getSessionId(), session);
SessionRenewLockLoop sessionRenewLockLoop = new SessionRenewLockLoop(session, this);
sessionRenewLockLoop.startLoop();
Expand Down Expand Up @@ -713,13 +728,34 @@ private void notifyExceptionToMessageHandler(Throwable ex, ExceptionPhase phase)

@Override
public int getPrefetchCount() {
this.checkInnerReceiveCreated();
return this.innerReceiver.getPrefetchCount();
return this.prefetchCount;
}

@Override
public void setPrefetchCount(int prefetchCount) throws ServiceBusException {
this.checkInnerReceiveCreated();
this.innerReceiver.setPrefetchCount(prefetchCount);
if(prefetchCount < 0)
{
throw new IllegalArgumentException("Prefetch count cannot be negative.");
}

this.prefetchCount = prefetchCount;
if(this.innerReceiver != null)
{
this.innerReceiver.setPrefetchCount(prefetchCount);
}

// For accepted session receivers also
IMessageSession[] currentAcceptedSessions = this.openSessions.values().toArray(new IMessageSession[0]);
for(IMessageSession session : currentAcceptedSessions)
{
try
{
session.setPrefetchCount(prefetchCount);
}
catch(IllegalStateException ise)
{
// Session might have been closed.. Ignore the exception as this is a best effort setter on already accepted sessions
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void setPrefetchCount(final int value) throws ServiceBusException
if(deltaPrefetchCount > 0)
{
try
{
{
this.underlyingFactory.scheduleOnReactorThread(new DispatchHandler()
{
@Override
Expand Down

0 comments on commit 2b8b400

Please sign in to comment.