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

标签 java callable method-interception

我正在为一个更大的项目(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/

相关文章:

Java并行流使用与否

java - 当我使用@Transactional时hibernateFilter。是必然的吗?

python - 如何产生 "Callable function"

javascript - 您可以向被劫持的 JavaScript 数组添加一个函数吗?

c# - Code Cop 应用程序挂起

java - 方法拦截以获取属性名称

java - 为什么重写方法总是从子类的对象调用?

java - 我怎样才能让这个正则表达式更短?

java - 如何从可运行的可调用对象中获取类名

python - 将可调用对象隐式绑定(bind)到实例