Search in sources :

Example 1 with PlainCallbackHandler

use of net.spy.memcached.auth.PlainCallbackHandler in project qi4j-sdk by Qi4j.

the class MemcachePoolMixin method activateService.

@Override
public void activateService() throws Exception {
    if (configuration != null) {
        MemcacheConfiguration config = configuration.get();
        expiration = (config.expiration().get() == null) ? 3600 : config.expiration().get();
        String addresses = (config.addresses().get() == null) ? "localhost:11211" : config.addresses().get();
        Protocol protocol = (config.protocol().get() == null) ? Protocol.TEXT : Protocol.valueOf(config.protocol().get().toUpperCase());
        String username = config.username().get();
        String password = config.password().get();
        String authMech = config.authMechanism().get() == null ? "PLAIN" : config.authMechanism().get();
        ConnectionFactoryBuilder builder = new ConnectionFactoryBuilder();
        builder.setProtocol(protocol);
        if (username != null && !username.isEmpty()) {
            builder.setAuthDescriptor(new AuthDescriptor(new String[] { authMech }, new PlainCallbackHandler(username, password)));
        }
        client = new MemcachedClient(builder.build(), AddrUtil.getAddresses(addresses));
    }
}
Also used : ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) Protocol(net.spy.memcached.ConnectionFactoryBuilder.Protocol)

Example 2 with PlainCallbackHandler

use of net.spy.memcached.auth.PlainCallbackHandler in project DataX by alibaba.

the class ConfigurationChecker method hostReachableCheck.

/**
     * 检查ocs服务器网络是否可达
     */
private static void hostReachableCheck(Configuration config) {
    String proxy = config.getString(Key.PROXY);
    String port = config.getString(Key.PORT);
    String username = config.getString(Key.USER);
    String password = config.getString(Key.PASSWORD);
    AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" }, new PlainCallbackHandler(username, password));
    try {
        MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(proxy + ":" + port));
        client.get("for_check_connectivity");
        client.getVersions();
        if (client.getAvailableServers().isEmpty()) {
            throw new RuntimeException("没有可用的Servers: getAvailableServers() -> is empty");
        }
        client.shutdown();
    } catch (Exception e) {
        throw DataXException.asDataXException(OcsWriterErrorCode.HOST_UNREACHABLE, String.format("OCS[%s]服务不可用", proxy), e);
    }
}
Also used : PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) DataXException(com.alibaba.datax.common.exception.DataXException)

Aggregations

ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)2 MemcachedClient (net.spy.memcached.MemcachedClient)2 AuthDescriptor (net.spy.memcached.auth.AuthDescriptor)2 PlainCallbackHandler (net.spy.memcached.auth.PlainCallbackHandler)2 DataXException (com.alibaba.datax.common.exception.DataXException)1 Protocol (net.spy.memcached.ConnectionFactoryBuilder.Protocol)1