Skip to content

Commit b4d0a6e

Browse files
committed
JAVA-682: Added three new classes--MongoClient, MongoClientOptions, and MongoClientURI, all of which have a default write concern that waits
for acknowledgement of the write from the server. MongoClient extends Mongo so that it's easy to switch to it just by changing which class is constructed. MongoClientOptions, unlike MongoOptions, is immutable and has a static inner Builder class for construction.
1 parent 3984340 commit b4d0a6e

13 files changed

+1775
-341
lines changed

src/main/com/mongodb/Mongo.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@
6464
* mongo.setWriteConcern(WriteConcern.SAFE);
6565
* </pre>
6666
*
67-
* @see com.mongodb.ReadPreference
68-
* @see com.mongodb.WriteConcern
67+
* Note: This class has been superseded by {@code MongoClient}, and may be deprecated in a future release.
68+
*
69+
* @see MongoClient
70+
* @see ReadPreference
71+
* @see WriteConcern
6972
*/
7073
public class Mongo {
7174

src/main/com/mongodb/MongoClient.java

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb;
19+
20+
import java.net.UnknownHostException;
21+
import java.util.List;
22+
23+
/**
24+
* A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance
25+
* for the entire JVM.
26+
* <p>
27+
* The following are equivalent, and all connect to the local database running on the default port:
28+
* <pre>
29+
* MongoClient mongoClient1 = new MongoClient();
30+
* MongoClient mongoClient1 = new MongoClient("localhost");
31+
* MongoClient mongoClient2 = new MongoClient("localhost", 27017);
32+
* MongoClient mongoClient4 = new MongoClient(new ServerAddress("localhost"));
33+
* MongoClient mongoClient5 = new MongoClient(new ServerAddress("localhost"), new MongoClientOptions.Builder().build());
34+
* </pre>
35+
* <p>
36+
* You can connect to a
37+
* <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a> using the Java driver by passing
38+
* a ServerAddress list to the MongoClient constructor. For example:
39+
* <pre>
40+
* MongoClient mongoClient = new MongoClient(Arrays.asList(
41+
* new ServerAddress("localhost", 27017),
42+
* new ServerAddress("localhost", 27018),
43+
* new ServerAddress("localhost", 27019)));
44+
* </pre>
45+
* You can connect to a sharded cluster using the same constructor. MongoClient will auto-detect whether the servers are
46+
* a list of replica set members or a list of mongos servers.
47+
* <p>
48+
* By default, all read and write operations will be made on the primary,
49+
* but it's possible to read from secondaries by changing the read preference:
50+
* <pre>
51+
* mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
52+
* </pre>
53+
* By default, all write operations will wait for acknowledgment by the server, as the default write concern is
54+
* {@code WriteConcern.ACKNOWLEDGED}.
55+
* <p>
56+
* Note: This class supersedes the {@code Mongo} class. While it extends {@code Mongo}, it differs from it in that
57+
* the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its
58+
* constructors accept instances of {@code MongoClientOptions} and {@code MongoClientURI}, which both also
59+
* set the same default write concern.
60+
*
61+
* @see ReadPreference#primary()
62+
* @see com.mongodb.WriteConcern#ACKNOWLEDGED
63+
* @see MongoClientOptions
64+
* @see MongoClientURI
65+
* @since 2.10.0
66+
*/
67+
public class MongoClient extends Mongo {
68+
69+
/**
70+
* Creates an instance based on a (single) mongodb node (localhost, default port).
71+
*
72+
* @throws UnknownHostException
73+
* @throws MongoException
74+
*/
75+
public MongoClient() throws UnknownHostException {
76+
super();
77+
setWriteConcern(WriteConcern.ACKNOWLEDGED);
78+
}
79+
80+
/**
81+
* Creates a Mongo instance based on a (single) mongodb node.
82+
*
83+
* @param host server to connect to in format host[:port]
84+
* @throws UnknownHostException if the database host cannot be resolved
85+
* @throws MongoException
86+
*/
87+
public MongoClient(String host) throws UnknownHostException {
88+
super(host);
89+
setWriteConcern(WriteConcern.ACKNOWLEDGED);
90+
}
91+
92+
/**
93+
* Creates a Mongo instance based on a (single) mongodb node (default port).
94+
*
95+
* @param host server to connect to in format host[:port]
96+
* @param options default query options
97+
* @throws UnknownHostException if the database host cannot be resolved
98+
* @throws MongoException
99+
*/
100+
public MongoClient(String host, MongoClientOptions options) throws UnknownHostException {
101+
super(host, new MongoOptions(options));
102+
}
103+
104+
/**
105+
* Creates a Mongo instance based on a (single) mongodb node.
106+
*
107+
* @param host the database's host address
108+
* @param port the port on which the database is running
109+
* @throws UnknownHostException if the database host cannot be resolved
110+
* @throws MongoException
111+
*/
112+
public MongoClient(String host, int port) throws UnknownHostException {
113+
super(host, port);
114+
setWriteConcern(WriteConcern.ACKNOWLEDGED);
115+
}
116+
117+
/**
118+
* Creates a Mongo instance based on a (single) mongodb node
119+
*
120+
* @param addr the database address
121+
* @throws MongoException
122+
* @see com.mongodb.ServerAddress
123+
*/
124+
public MongoClient(ServerAddress addr) {
125+
super(addr);
126+
setWriteConcern(WriteConcern.ACKNOWLEDGED);
127+
}
128+
129+
/**
130+
* Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.
131+
*
132+
* @param addr the database address
133+
* @param options default options
134+
* @throws MongoException
135+
* @see com.mongodb.ServerAddress
136+
*/
137+
public MongoClient(ServerAddress addr, MongoClientOptions options) {
138+
super(addr, new MongoOptions(options));
139+
}
140+
141+
/**
142+
* Creates a Mongo based on a list of replica set members or a list of mongos.
143+
* It will find all members (the master will be used by default). If you pass in a single server in the list,
144+
* the driver will still function as if it is a replica set. If you have a standalone server,
145+
* use the Mongo(ServerAddress) constructor.
146+
* <p/>
147+
* If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
148+
* and automatically fail over to the next server if the closest is down.
149+
*
150+
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
151+
* either be a list of mongod servers in the same replica set or a list of mongos servers in the same
152+
* sharded cluster.
153+
* @throws MongoException
154+
* @see com.mongodb.ServerAddress
155+
*/
156+
public MongoClient(List<ServerAddress> seeds) {
157+
super(seeds);
158+
setWriteConcern(WriteConcern.ACKNOWLEDGED);
159+
}
160+
161+
162+
/**
163+
* Creates a Mongo based on a list of replica set members or a list of mongos.
164+
* It will find all members (the master will be used by default). If you pass in a single server in the list,
165+
* the driver will still function as if it is a replica set. If you have a standalone server,
166+
* use the Mongo(ServerAddress) constructor.
167+
* <p/>
168+
* If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
169+
* and automatically fail over to the next server if the closest is down.
170+
*
171+
* @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
172+
* either be a list of mongod servers in the same replica set or a list of mongos servers in the same
173+
* sharded cluster.
174+
* @param options default options
175+
* @throws MongoException
176+
* @see com.mongodb.ServerAddress
177+
*/
178+
public MongoClient(List<ServerAddress> seeds, MongoClientOptions options) {
179+
super(seeds, new MongoOptions(options));
180+
}
181+
182+
183+
/**
184+
* Creates a Mongo described by a URI.
185+
* If only one address is used it will only connect to that node, otherwise it will discover all nodes.
186+
* @param uri the URI
187+
* @throws MongoException
188+
* @throws UnknownHostException
189+
* @see MongoURI
190+
* @dochub connections
191+
*/
192+
public MongoClient(MongoClientURI uri) throws UnknownHostException {
193+
super(new MongoURI(uri));
194+
}
195+
}

0 commit comments

Comments
 (0)