Skip to content

Instantly share code, notes, and snippets.

@daschl
Created March 21, 2014 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daschl/9681338 to your computer and use it in GitHub Desktop.
Save daschl/9681338 to your computer and use it in GitHub Desktop.
package com.couchbase;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.CouchbaseConnectionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.net.URI;
import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
@Configuration
@EnableAutoConfiguration
@Controller
public class Application {
@Autowired
private CouchbaseClient couchbaseClient;
@Bean
public CouchbaseConnectionFactoryBuilder couchbaseConnectionFactoryBuilder() throws Exception {
CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
builder
.setOpTimeout(2500)
.setTimeoutExceptionThreshold(998)
.setReadBufferSize(16384)
.setOpQueueMaxBlockTime(10000)
.setShouldOptimize(false)
.setMaxReconnectDelay(30000);
return builder;
}
@Bean
public CouchbaseClient couchbaseClient() throws Exception {
return new CouchbaseClient(couchbaseConnectionFactoryBuilder().buildCouchbaseConnection(
Arrays.asList(URI.create("http://192.168.56.103:8091/pools")),
"protected",
"secret"
));
}
@RequestMapping("/")
@ResponseBody
String index() {
String key = UUID.randomUUID().toString();
String value = "content";
try {
couchbaseClient.set(key, value).get();
} catch (Exception e) {
e.printStackTrace();
}
Object found = couchbaseClient.get(key);
return found.toString();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment