Skip to content

Instantly share code, notes, and snippets.

@jyemin
Created December 21, 2015 18:02
Show Gist options
  • Save jyemin/54caedca176c014fa659 to your computer and use it in GitHub Desktop.
Save jyemin/54caedca176c014fa659 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.
*
*/
package tour;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.event.ClusterDescriptionChangedEvent;
import com.mongodb.event.ClusterListenerAdapter;
import org.bson.Document;
import static java.util.Arrays.asList;
public class ClusterEventsTest {
private static volatile boolean canWrite = false;
private static volatile boolean canRead = true;
private static final ReadPreference readPreference = ReadPreference.secondary();
public static void main(String[] args) throws InterruptedException {
MongoClientOptions options =
MongoClientOptions.builder()
.addClusterListener(new ClusterListenerAdapter() {
@Override
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
boolean couldWrite = canWrite;
canWrite = !event.getClusterDescription().getPrimaries().isEmpty();
if (couldWrite && !canWrite) {
System.out.println("CAN'T WRITE ANYMORE!!");
} else if (!couldWrite && canWrite) {
System.out.println("CAN WRITE NOW!!!");
}
}
})
.addClusterListener(new ClusterListenerAdapter() {
@Override
public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
boolean couldRead = canRead;
canRead = !readPreference.choose(event.getClusterDescription()).isEmpty();
if (couldRead && !canRead) {
System.out.println("CAN'T READ ANYMORE!!");
} else if (!couldRead && canRead) {
System.out.println("CAN READ NOW!!!");
}
}
})
.build();
MongoClient client = new MongoClient(asList(new ServerAddress(), new ServerAddress("localhost:27018")), options);
MongoCollection<Document> collection = client.getDatabase("test").getCollection("test");
new Thread(() -> {
while (true) {
if (canWrite) {
try {
collection.insertOne(new Document());
} catch (Exception e) {
System.out.println("Exception while writing: " + e);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore interruptions
}
}
});
new Thread(() -> {
while (true) {
if (canRead) {
try {
collection.withReadPreference(readPreference).find().first();
} catch (Exception e) {
System.out.println("Exception while writing: " + e);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore interruptions
}
}
});
Thread.sleep(Long.MAX_VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment