Search in sources :

Example 1 with MethodLookup

use of org.jgroups.blocks.MethodLookup in project teiid by teiid.

the class JGroupsObjectReplicator method replicate.

@Override
public <T, S> T replicate(String mux_id, Class<T> iface, final S object, long startTimeout) throws Exception {
    Channel channel = channelFactory.createChannel(mux_id);
    // To keep the order of methods same at all the nodes.
    TreeMap<String, Method> methods = new TreeMap<String, Method>();
    for (Method method : iface.getMethods()) {
        if (method.getAnnotation(Replicated.class) == null) {
            continue;
        }
        methods.put(method.toGenericString(), method);
    }
    final HashMap<Method, Short> methodMap = new HashMap<Method, Short>();
    final ArrayList<Method> methodList = new ArrayList<Method>();
    for (String method : methods.keySet()) {
        methodList.add(methods.get(method));
        methodMap.put(methods.get(method), (short) (methodList.size() - 1));
    }
    Method hasState = ReplicatedObject.class.getMethod(HAS_STATE, new Class<?>[] { Serializable.class });
    methodList.add(hasState);
    methodMap.put(hasState, (short) (methodList.size() - 1));
    Method sendState = JGroupsObjectReplicator.Streaming.class.getMethod(SEND_STATE, new Class<?>[] { Serializable.class, Address.class });
    methodList.add(sendState);
    methodMap.put(sendState, (short) (methodList.size() - 1));
    // add in streaming methods
    Method createState = JGroupsObjectReplicator.Streaming.class.getMethod(CREATE_STATE, new Class<?>[] { Serializable.class });
    methodList.add(createState);
    methodMap.put(createState, (short) (methodList.size() - 1));
    Method buildState = JGroupsObjectReplicator.Streaming.class.getMethod(BUILD_STATE, new Class<?>[] { Serializable.class, byte[].class });
    methodList.add(buildState);
    methodMap.put(buildState, (short) (methodList.size() - 1));
    Method finishState = JGroupsObjectReplicator.Streaming.class.getMethod(FINISH_STATE, new Class<?>[] { Serializable.class });
    methodList.add(finishState);
    methodMap.put(finishState, (short) (methodList.size() - 1));
    ReplicatedInvocationHandler<S> proxy = new ReplicatedInvocationHandler<S>(object, methodMap);
    /*
         * TODO: could have an object implement streaming
         * Override the normal handle method to support streaming
         */
    ReplicatorRpcDispatcher disp = new ReplicatorRpcDispatcher<S>(channel, proxy, proxy, object, object, methodMap, methodList);
    proxy.setDisp(disp);
    disp.setMethodLookup(new MethodLookup() {

        public Method findMethod(short id) {
            return methodList.get(id);
        }
    });
    T replicatedProxy = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { iface }, proxy);
    boolean success = false;
    try {
        channel.connect(mux_id);
        if (object instanceof ReplicatedObject) {
            ((ReplicatedObject) object).setAddress(channel.getAddress());
            proxy.pullState(null, null, null, startTimeout, startTimeout != 0 ? STATE_TIMEOUT : 0);
        }
        success = true;
        return replicatedProxy;
    } catch (Throwable e) {
        if (e instanceof Exception) {
            throw (Exception) e;
        }
        throw new TeiidRuntimeException(RuntimePlugin.Event.TEIID40104, e);
    } finally {
        if (!success) {
            channel.close();
        } else {
            synchronized (disp) {
                // mark as initialized so that state can be pulled if needed
                disp.initialized = true;
            }
        }
    }
}
Also used : Replicated(org.teiid.Replicated) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) MethodLookup(org.jgroups.blocks.MethodLookup) Channel(org.jgroups.Channel) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReplicatedObject(org.teiid.query.ReplicatedObject)

Aggregations

IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Channel (org.jgroups.Channel)1 MethodLookup (org.jgroups.blocks.MethodLookup)1 Replicated (org.teiid.Replicated)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 ReplicatedObject (org.teiid.query.ReplicatedObject)1