I am new to memcached and Spring MVC, I am trying to create a rest api which would be setting some values in memcached. Which I can retrieve also. But I am not getting the right way to connect to memcached.
Consumerconfiguration:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import com.btmatthews.springboot.memcached.EnableMemcached;
@EnableMemcached
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "msg91.consumers.consumer")
public class consumerConfiguration {
}
ConsumerInitializer class :
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class consumerInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { consumerConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Controller class:
@RestController
public class consumerController {
@Autowired
private MemcachedClient memcachedClient;
@RequestMapping(value="/queues/{queueName}/thread-counts/{threadCount}", method = RequestMethod.POST)
public void setThreadCount(@PathVariable String queueName, @PathVariable int threadCount) {
System.out.println("Queue name is "+queueName);
System.out.println("Thread count is "+threadCount);
memcachedClient.set(queueName, 0, threadCount);//Setting the values in memcache which I get in api
}
@RequestMapping(value="/queues/{queueName}/thread-counts", method = RequestMethod.GET)
public void getThreadCount(@PathVariable String queueName) {
System.out.println("Queue value is"+queueName);
int threadCount = (Integer)memcachedClient.get(queueName);
System.out.println("Thread count for " + queueName + " is " + threadCount);
}
}
Spring-dispatcher-servlet.xml:
<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
<property name="cacheClientFactory">
<bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="127.0.0.1:11211" />
</bean>
</property>
It is not working, giving error:
Servlet [spring-dispatcher] in web application [/consumer-api] threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
I think I am not able to give the path of memcache properly or not able to connect to memcached. Please help me to resolve this issue.