public static String getStringObject(byte[] bytes) {
   try {
     Marshaller sm = new JBossMarshaller();
     return (String) sm.objectFromByteBuffer(bytes);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
 @Override
 public Object getKey() {
   String result = String.valueOf(r.nextLong());
   Marshaller sm = new JBossMarshaller();
   try {
     return sm.objectToByteBuffer(result, 64);
   } catch (IOException e) {
     throw new RuntimeException(e);
   } catch (InterruptedException e) {
     throw new RuntimeException(e);
   }
 }
Beispiel #3
0
  /**
   * Clones parameter x of type T with a given Marshaller reference;
   *
   * @return a deep clone of an object parameter x
   */
  @SuppressWarnings("unchecked")
  public static <T> T cloneWithMarshaller(Marshaller marshaller, T x) {
    if (marshaller == null)
      throw new IllegalArgumentException("Cannot use null Marshaller for clone");

    byte[] byteBuffer;
    try {
      byteBuffer = marshaller.objectToByteBuffer(x);
      return (T) marshaller.objectFromByteBuffer(byteBuffer);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new CacheException(e);
    } catch (Exception e) {
      throw new CacheException(e);
    }
  }
    @Override
    public Object unboxValue(Object target) {
      if (marshaller != null && target instanceof byte[]) {
        try {
          return marshaller.objectFromByteBuffer((byte[]) target);
        } catch (Exception e) {
          throw new CacheException("Unable to unmarshall return value");
        }
      }

      return target;
    }
  private Object generateKeyAndShutdownServer()
      throws IOException, ClassNotFoundException, InterruptedException {
    resetStats();
    Cache<Object, Object> cache = manager(1).getCache();
    ExecutorService ex = Executors.newSingleThreadExecutor(getTestThreadFactory("KeyGenerator"));
    KeyAffinityService kaf =
        KeyAffinityServiceFactory.newKeyAffinityService(cache, ex, new ByteKeyGenerator(), 2, true);
    Address address = cache.getAdvancedCache().getRpcManager().getTransport().getAddress();
    byte[] keyBytes = (byte[]) kaf.getKeyForAddress(address);
    String key = ByteKeyGenerator.getStringObject(keyBytes);
    ex.shutdownNow();
    kaf.stop();

    remoteCache.put(key, "v");
    assertOnlyServerHit(getAddress(hotRodServer2));
    TcpTransportFactory tcpTp =
        (TcpTransportFactory)
            ((InternalRemoteCacheManager) remoteCacheManager).getTransportFactory();

    Marshaller sm = new JBossMarshaller();
    TcpTransport transport =
        (TcpTransport)
            tcpTp.getTransport(
                sm.objectToByteBuffer(key, 64), null, RemoteCacheManager.cacheNameBytes());
    try {
      assertEquals(
          transport.getServerAddress(),
          new InetSocketAddress("localhost", hotRodServer2.getPort()));
    } finally {
      tcpTp.releaseTransport(transport);
    }

    log.info("About to stop Hot Rod server 2");
    hotRodServer2.stop();

    return key;
  }