@Override public RestStoreConfigurationBuilder read(RestStoreConfiguration template) { super.read(template); this.connectionPool.read(template.connectionPool()); return this; }
@Override public void clear() { DefaultHttpRequest delete = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, path); try { Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast(new HttpResponseHandler()).channel(); ch.writeAndFlush(delete).sync().channel().closeFuture().sync(); } catch (Exception e) { throw new PersistenceException(e); } }
public RestStoreConfigurationBuilder(PersistenceConfigurationBuilder builder) { super(builder, RestStoreConfiguration.attributeDefinitionSet()); connectionPool = new ConnectionPoolConfigurationBuilder(this); }
public void testRemoteCacheStore() throws Exception { cacheManager = TestCacheManagerFactory.fromXml(CACHE_LOADER_CONFIG); List<StoreConfiguration> cacheLoaders = cacheManager.getDefaultCacheConfiguration().persistence().stores(); assertEquals(1, cacheLoaders.size()); RestStoreConfiguration store = (RestStoreConfiguration) cacheLoaders.get(0); assertFalse(store.appendCacheNameToPath()); assertEquals("localhost", store.host()); assertEquals("/rest/___defaultcache/", store.path()); assertEquals(18212, store.port()); assertEquals(15000000, store.maxContentLength()); ConnectionPoolConfiguration connectionPool = store.connectionPool(); assertEquals(10000, connectionPool.connectionTimeout()); assertEquals(10, connectionPool.maxConnectionsPerHost()); assertEquals(10, connectionPool.maxTotalConnections()); assertEquals(20000, connectionPool.bufferSize()); assertEquals(10000, connectionPool.socketTimeout()); assertTrue(connectionPool.tcpNoDelay()); assertFalse(store.async().enabled()); } }
ConnectionPoolConfiguration pool = configuration.connectionPool(); workerGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class); b.option(ChannelOption.TCP_NODELAY, pool.tcpNoDelay()); bootstrap = b; maxContentLength = configuration.maxContentLength(); this.key2StringMapper = Util.getInstance(configuration.key2StringMapper(), ctx.getCache().getAdvancedCache().getClassLoader()); this.key2StringMapper.setMarshaller(ctx.getMarshaller()); this.path = configuration.path(); try { if (configuration.appendCacheNameToPath()) { path = path + urlCodec.encode(ctx.getCache().getName()) + "/"; this.metadataHelper = Util.getInstance(configuration.metadataHelper(), ctx.getCache().getAdvancedCache().getClassLoader());
@Override public void serialize(XMLExtendedStreamWriter writer, RestStoreConfiguration configuration) throws XMLStreamException { writer.writeStartElement(Element.REST_STORE); configuration.attributes().write(writer); writeCommonStoreSubAttributes(writer, configuration); writeServer(writer, configuration); writeConnectionPool(writer, configuration.connectionPool()); writeCommonStoreElements(writer, configuration); writer.writeEndElement(); }
@Override public RestStoreConfiguration create() { return new RestStoreConfiguration(attributes.protect(), async.create(), singletonStore.create(), connectionPool.create()); }
@Override public boolean isAvailable() { try { DefaultHttpRequest get = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpResponseHandler handler = new HttpResponseHandler(true); Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast( new HttpObjectAggregator(maxContentLength), handler).channel(); ch.writeAndFlush(get).sync().channel().closeFuture().sync(); return handler.getResponse().status().code() == 200; } catch (Exception e) { return false; } }
@Override protected void compareStoreConfiguration(String name, StoreConfiguration beforeStore, StoreConfiguration afterStore) { super.compareStoreConfiguration(name, beforeStore, afterStore); RestStoreConfiguration before = (RestStoreConfiguration) beforeStore; RestStoreConfiguration after = (RestStoreConfiguration) afterStore; AssertJUnit.assertEquals("Wrong connection pool for " + name + " configuration.", before.connectionPool(), after.connectionPool()); } }
private void writeServer(XMLExtendedStreamWriter writer, RestStoreConfiguration configuration) throws XMLStreamException { writer.writeStartElement(Element.SERVER); writer.writeAttribute(Attribute.HOST, configuration.host()); writer.writeAttribute(Attribute.PORT, Integer.toString(configuration.port())); writer.writeEndElement(); }
@Override public boolean delete(Object key) { DefaultHttpRequest delete = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, keyToUri(key)); try { HttpResponseHandler handler = new HttpResponseHandler(true); Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast(new HttpObjectAggregator(maxContentLength), handler).channel(); ch.writeAndFlush(delete).sync().channel().closeFuture().sync(); try { return isSuccessful(handler.getResponse().status().code()); } finally { handler.getResponse().release(); } } catch (Exception e) { throw new PersistenceException(e); } }
@Override public int size() { Channel ch = null; DefaultHttpRequest get = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path + "?global"); get.headers().add(HttpHeaders.Names.ACCEPT, "text/plain"); try { HttpResponseHandler handler = new HttpResponseHandler(true); ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast( new HttpObjectAggregator(maxContentLength), handler).channel(); ch.writeAndFlush(get).sync().channel().closeFuture().sync(); try { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(new ByteBufInputStream(((FullHttpResponse) handler.getResponse()).content()))); int count = 0; while (reader.readLine() != null) count++; return count; } finally { reader.close(); } } finally { handler.getResponse().release(); } } catch (Exception e) { throw log.errorLoadingRemoteEntries(e); } }
@Override public void write(MarshalledEntry entry) { try { String contentType = metadataHelper.getContentType(entry); ByteBuf content = Unpooled.wrappedBuffer(marshall(contentType, entry)); DefaultFullHttpRequest put = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, keyToUri(entry.getKey()), content); put.headers().add("Content-Type", contentType); put.headers().add("Content-Length", content.readableBytes()); InternalMetadata metadata = entry.getMetadata(); if (metadata != null && metadata.expiryTime() > -1) { put.headers().add(TIME_TO_LIVE_SECONDS, Long.toString(timeoutToSeconds(metadata.lifespan()))); put.headers().add(MAX_IDLE_TIME_SECONDS, Long.toString(timeoutToSeconds(metadata.maxIdle()))); } Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast(new HttpResponseHandler()).channel(); ch.writeAndFlush(put).sync().channel().closeFuture().sync(); } catch (Exception e) { throw new PersistenceException(e); } }
@Override public Flowable<K> publishKeys(Predicate<? super K> filter) { return Flowable.using(() -> { DefaultHttpRequest get = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path + "?global"); get.headers().add(HttpHeaderNames.ACCEPT, "text/plain"); get.headers().add(HttpHeaderNames.ACCEPT_CHARSET, "UTF-8"); HttpResponseHandler handler = new HttpResponseHandler(true); try { Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast( new HttpObjectAggregator(maxContentLength), handler).channel(); ch.writeAndFlush(get).sync().channel().closeFuture().sync(); BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteBufInputStream((handler.getResponse()).content()), StandardCharsets.UTF_8)); // Response can't be null now return new KeyValuePair<>(handler.getResponse(), reader); } catch (Throwable t) { FullHttpResponse response = handler.getResponse(); if (response != null) { response.release(); } throw t; } }, kvp -> Flowable.fromIterable(() -> new RestIterator(kvp, filter)), kvp -> { try { kvp.getValue().close(); } finally { kvp.getKey().release(); } }); }
Channel ch = bootstrap.connect(configuration.host(), configuration.port()).awaitUninterruptibly().channel().pipeline().addLast(new HttpObjectAggregator(maxContentLength), handler).channel(); ch.writeAndFlush(get).sync().channel().closeFuture().sync(); FullHttpResponse response = handler.getResponse();