Java tutorial
/* * Copyright (c) 2013 ITOCHU Techno-Solutions Corporation. * * 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 jp.co.ctc_g.rack.core.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import com.mongodb.Mongo; import com.mongodb.MongoOptions; import com.mongodb.ServerAddress; /** * <p> * ????RabbitMQ???? * </p> * @author ITOCHU Techno-Solutions Corporation. */ @Configuration public class RackMongoContextConfig { @Value("${MONGO_HOST:127.0.0.1}") private String host; @Value("${MONGO_PORT:27017}") private int port; @Value("${MONGO_CONNECTIONS_PER_HOST:8}") private int connectionsPerHost; @Value("${MONGO_THREAD_ALLOWED_TO_BLOCK_FOR_CONNECTION_MULTIPLIER:4}") private int threadAllowedToBlockForConnectionMultiplier; @Value("${MONGO_CONNECT_TIMEOUT:1000}") private int connectTimeout; @Value("${MONGO_MAX_WAIT_TIME:1500}") private int maxWaitTime; @Value("${MONGO_AUTO_CONNECT_RETRY:true}") private boolean autoConnectRetry; @Value("${MONGO_SOCKET_KEEP_ALIVE:true}") private boolean socketKeepAlive; @Value("${MONGO_SOCKE_TTIMEOUT:6000}") private int socketTimeout; @Value("${MONGO_FSYNC:true}") private boolean fsync = true; @Value("${MONGO_DATABASENAME:openstack}") private String databaseName; /** * ?? */ public RackMongoContextConfig() { } /** * MongoDI???? * @return Mongo */ @Bean public MongoOptions mongoOptions() { MongoOptions options = new MongoOptions(); options.setConnectionsPerHost(connectionsPerHost); options.setThreadsAllowedToBlockForConnectionMultiplier(threadAllowedToBlockForConnectionMultiplier); options.setConnectTimeout(connectTimeout); options.setMaxWaitTime(maxWaitTime); options.setAutoConnectRetry(autoConnectRetry); options.setSocketKeepAlive(socketKeepAlive); options.setSocketTimeout(socketTimeout); options.setFsync(fsync); return options; } /** * MongoDI???? * @return Mongo * @throws Exception */ @Bean public Mongo mongo() throws Exception { return new Mongo(new ServerAddress(host, port), mongoOptions()); } /** * MongoDB?DI???? * @return SimpleMongoDbFactory? * @throws Exception */ @Bean public MongoDbFactory mongoDbFactory() throws Exception { return new SimpleMongoDbFactory(mongo(), databaseName); } /** * MongoDI???? * @return Mongo * @throws Exception */ @Bean public MongoOperations mongoOperations() throws Exception { return new MongoTemplate(mongoDbFactory()); } }