Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created January 30, 2023 21:17
Show Gist options
  • Save jyemin/1151e3a72c1ff835433f0c88e2d07863 to your computer and use it in GitHub Desktop.
Save jyemin/1151e3a72c1ff835433f0c88e2d07863 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import com.mongodb.connection.ConnectionId;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
import com.mongodb.event.CommandSucceededEvent;
import org.bson.BsonDocument;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
public class InProgressTrackingCommandListener implements CommandListener {
public static class TrackingData {
private final long requestId;
private final ConnectionId connectionId;
private final String databaseName;
private final String commandName;
private final BsonDocument command;
TrackingData(CommandStartedEvent event) {
requestId = event.getRequestId();
connectionId = event.getConnectionDescription().getConnectionId();
databaseName = event.getDatabaseName();
commandName = event.getCommandName();
command = event.getCommand().clone();
}
public long getRequestId() {
return requestId;
}
public ConnectionId getConnectionId() {
return connectionId;
}
public String getDatabaseName() {
return databaseName;
}
public String getCommandName() {
return commandName;
}
public BsonDocument getCommand() {
return command;
}
}
private final ConcurrentHashMap<Integer, TrackingData> inProgressCommands = new ConcurrentHashMap<>();
public Collection<TrackingData> getInProgressCommandsTrackingData() {
return new ArrayList<>(inProgressCommands.values());
}
@Override
public void commandStarted(CommandStartedEvent event) {
inProgressCommands.put(event.getRequestId(), new TrackingData(event));
}
@Override
public void commandSucceeded(CommandSucceededEvent event) {
inProgressCommands.remove(event.getRequestId());
}
@Override
public void commandFailed(CommandFailedEvent event) {
inProgressCommands.remove(event.getRequestId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment