gpt4 book ai didi

java - 如何劫持 Callable 并在 call() 之前执行隐藏方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:27 24 4
gpt4 key购买 nike

我正在为一个更大的项目(HypergraphDB)中的一些子系统添加实现,我应该避免修改重要的代码。在这个项目中,大约有 70 个 Callable 对象,它们定义了一些数据库操作的事务 block 。我正在替换该数据库,并且我的(redis)在定义事务 block 之前需要所有受影响的键。因此,我需要访问这些 Callables 中的部分,并在执行 call() 之前执行某些操作(“watch”)。

一些 Callable 非常重要,对于其中一些,我需要在该 Callable 中声明最终字段,并将它们定义在调用的“外部”,以便 call() 和 getHandles() 都可以访问它们。

为了能够访问 getHandles 方法,我定义了一个仅包含 getHandles 方法的接口(interface)。要访问 getHandles,我可以临时将 Callable 转换为该接口(interface),现在在 IDE 中它“看到”该方法,但 atm 我无法测试它。

这行得通吗?

之前的代码:

       private HGLiveHandle addLink(final Object payload, 
final HGHandle typeHandle,
final HGLink outgoingSet,
final byte flags)
{
return getTransactionManager().ensureTransaction(new Callable<HGLiveHandle>()
{ public HGLiveHandle call() {
HGAtomType type = typeSystem.getType(typeHandle);
HGPersistentHandle pTypeHandle = getPersistentHandle(typeHandle);
HGPersistentHandle valueHandle = TypeUtils.storeValue(HyperGraph.this, payload, type);
......
}

之后的代码:

         private HGLiveHandle addLink(final Object payload, 
final HGHandle typeHandle,
final HGLink outgoingSet,
final byte flags)
{
return getTransactionManager().ensureTransaction(new Callable<HGLiveHandle>()
{

final HGAtomType type = typeSystem.getType(typeHandle);
final HGPersistentHandle pTypeHandle = getPersistentHandle(typeHandle);
final HGPersistentHandle valueHandle = TypeUtils.storeValue(HyperGraph.this, payload, type);

public HGPersistentHandle[] getHandles() {
HGPersistentHandle[] result = new HGPersistentHandle[3+outgoingSet.getArity()];
result[0] = atomHandle.getPersistent();
result[1] = typeHandle.getPersistent();
result[2] = valueHandle.getPersistent();
result[3] = pTypeHandle;
result[4] = .valueHandle;
return result;
}

public HGLiveHandle call() {.........

界面可操作:

     public interface Handable {

public HGPersistentHandle[] getHandles();

}

最后是它被调用的地方:

     public <V> V ensureTransaction(Callable<V> transaction, HGTransactionConfig config)
{
if (getContext().getCurrent() != null)
try
{
for (HGPersistentHandle ph: ((Handable) transaction).getHandles());
.... writeJedis.watch(ph.toByteArray);
return transaction.call();

最佳答案

如果您确实想拦截 Callables,而不是修改现有代码,那么最好的选择是在调用 getTransactionManager() 时交换不同的实现,因为这是您传递 Callables 的地方。

public class MyTransactionManager implements TransactionManager {

private final TransactionManager originalManager = ...;

public <V> V ensureTransaction(Callable<V> transaction, HGTransactionConfig config) {
// Check for appropriate type
if( transaction instanceof Handable) {
for (HGPersistentHandle ph: ((Handable) transaction).getHandles()) {
writeJedis.watch(ph.toByteArray);
}
}
// Delegate to the original implementation
return originalManager.ensureTransaction(transaction, config);
}

}

关于java - 如何劫持 Callable 并在 call() 之前执行隐藏方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com