For IntelliJ IDEA,
Android Studio or Eclipse



@Override public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } }
@Override public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception { for (Map.Entry<JobInstance, List<Integer>> entry : shardingResults.entrySet()) { for (int shardingItem : entry.getValue()) { curatorTransactionFinal.create().forPath(jobNodePath.getFullPath(ShardingNode.getInstanceNode(shardingItem)), entry.getKey().getJobInstanceId().getBytes()).and(); } } curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.NECESSARY)).and(); curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.PROCESSING)).and(); } }
@Override public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } }
/** * Unannounces an announcement created at path. Note that if all announcements get removed, the Announcer * will continue to have ZK watches on paths because clearing them out is a source of ugly race conditions. * <p/> * If you need to completely clear all the state of what is being watched and announced, stop() the Announcer. * * @param path the path to unannounce */ public void unannounce(String path) { log.info("unannouncing [%s]", path); final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path); final String parentPath = pathAndNode.getPath(); final ConcurrentMap<String, byte[]> subPaths = announcements.get(parentPath); if (subPaths == null || subPaths.remove(pathAndNode.getNode()) == null) { log.error("Path[%s] not announced, cannot unannounce.", path); return; } try { curator.inTransaction().delete().forPath(path).and().commit(); } catch (KeeperException.NoNodeException e) { log.info("node[%s] didn't exist anyway...", path); } catch (Exception e) { throw Throwables.propagate(e); } }
/** * 在事务中执行操作. * * @param callback 执行操作的回调 */ public void executeInTransaction(final TransactionExecutionCallback callback) { try { CuratorTransactionFinal curatorTransactionFinal = getClient().inTransaction().check().forPath("/").and(); callback.execute(curatorTransactionFinal); curatorTransactionFinal.commit(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } }
private static CuratorTransactionFinal modifyZkRules( CuratorTransactionFinal transactionFinal,String ruleName ,List<String> newDataNodes ) throws Exception { CuratorFramework client= ZKUtils.getConnection(); String rulePath= ZKUtils.getZKBasePath() + "rules/function"; JSONArray jsonArray= JSON.parseArray(new String(client.getData().forPath(rulePath) ,"UTF-8")) ; for (Object obj: jsonArray) { JSONObject func= (JSONObject) obj; if(ruleName.equalsIgnoreCase(func.getString("name"))) { JSONArray property= func.getJSONArray("property") ; for (Object o : property) { JSONObject count= (JSONObject) o; if("count".equals(count.getString("name"))){ Integer xcount=Integer.parseInt( count.getString("value")) ; count.put("value",String.valueOf(xcount+newDataNodes.size())) ; if(transactionFinal==null){ transactionFinal= ZKUtils.getConnection().inTransaction().setData().forPath(rulePath, JSON.toJSONBytes(jsonArray)).and(); } else { transactionFinal.setData().forPath(rulePath, JSON.toJSONBytes(jsonArray)); } } } } } return transactionFinal; }
final CuratorOp deleteOp = curator.transactionOp().delete().forPath(testPath1); final Collection<CuratorTransactionResult> results = curator.transaction().forOperations(deleteOp); Assert.assertEquals(1, results.size()); final CuratorTransactionResult result = results.iterator().next(); Assert.assertEquals(Code.OK.intValue(), result.getError()); // assert delete
transactionFinal= client.inTransaction() .setData().forPath(taskPath,JSON.toJSONBytes(taskNode)).and() ; List<MigrateTask> value=entry.getValue(); String path= taskPath + "/" + key; transactionFinal= transactionFinal.create().forPath(path, JSON.toJSONBytes(value)).and() ; transactionFinal.commit(); } catch (Exception e) { LOGGER.error("migrate error", e);
@Override public void update(final String key, final String value) { try { client.inTransaction().check().forPath(key).and().setData().forPath(key, value.getBytes(Charsets.UTF_8)).and().commit(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } }
@Override public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception { for (Map.Entry<JobInstance, List<Integer>> entry : shardingResults.entrySet()) { for (int shardingItem : entry.getValue()) { curatorTransactionFinal.create().forPath(jobNodePath.getFullPath(ShardingNode.getInstanceNode(shardingItem)), entry.getKey().getJobInstanceId().getBytes()).and(); } } curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.NECESSARY)).and(); curatorTransactionFinal.delete().forPath(jobNodePath.getFullPath(ShardingNode.PROCESSING)).and(); } }
/** * 在事务中执行操作. * * @param callback 执行操作的回调 */ public void executeInTransaction(final TransactionExecutionCallback callback) { try { CuratorTransactionFinal curatorTransactionFinal = getClient().inTransaction().check().forPath("/").and(); callback.execute(curatorTransactionFinal); curatorTransactionFinal.commit(); //CHECKSTYLE:OFF } catch (final Exception ex) { //CHECKSTYLE:ON RegExceptionHandler.handleException(ex); } }