Ejemplo n.º 1
0
  public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IAtomicLong atomicLong = hz.getAtomicLong("counter");

    atomicLong.set(1);
    long result = atomicLong.apply(new Add2Function());
    System.out.println("apply.result:" + result);
    System.out.println("apply.value:" + atomicLong.get());

    atomicLong.set(1);
    atomicLong.alter(new Add2Function());
    System.out.println("alter.value:" + atomicLong.get());

    atomicLong.set(1);
    result = atomicLong.alterAndGet(new Add2Function());
    System.out.println("alterAndGet.result:" + result);
    System.out.println("alterAndGet.value:" + atomicLong.get());

    atomicLong.set(1);
    result = atomicLong.getAndAlter(new Add2Function());
    System.out.println("getAndAlter.result:" + result);
    System.out.println("getAndAlter.value:" + atomicLong.get());

    System.exit(0);

    for (; ; ) {
      long oldValue = atomicLong.get();
      long newValue = oldValue + 2;
      if (atomicLong.compareAndSet(oldValue, newValue)) {
        break;
      }
    }
  }
  @Setup
  public void setup(TestContext testContext) throws Exception {
    this.testContext = testContext;
    HazelcastInstance targetInstance = testContext.getTargetInstance();

    produced = targetInstance.getAtomicLong(basename + "-" + testContext.getTestId() + ":Produced");
    consumed = targetInstance.getAtomicLong(basename + "-" + testContext.getTestId() + ":Consumed");
    workQueue = targetInstance.getQueue(basename + "-" + testContext.getTestId() + ":WorkQueue");
  }
  @Before
  public void setUp() {
    super.setUp();

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getNetworkConfig().setRedoOperation(true);
    client = HazelcastClient.newHazelcastClient(clientConfig);
    references = new IAtomicLong[REFERENCE_COUNT];
    for (int k = 0; k < references.length; k++) {
      references[k] = client.getAtomicLong("atomicreference:" + k);
    }

    stressThreads = new StressThread[CLIENT_THREAD_COUNT];
    for (int k = 0; k < stressThreads.length; k++) {
      stressThreads[k] = new StressThread();
      stressThreads[k].start();
    }
  }
  @RequestMapping(
      consumes = "application/json",
      method = RequestMethod.POST,
      produces = "application/json")
  @ResponseBody
  public Long submitOrder(@RequestBody OrderRequest request) throws Exception {
    IAtomicLong atomicLong = hazelcastClient.getAtomicLong(ORDER_ID_SEQUENCE);
    Long id = atomicLong.getAndIncrement();

    // We could change this to get the sum total of all the books
    Collection<Book> books = booksService.getBooks(request.getIsbns());
    BigDecimal result =
        books.stream().map(book -> book.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);

    Order order =
        new Order(id, request.getCustomer().getId(), request.getIsbns(), new Date(), result);
    ordersService.addOrder(order);
    return id;
  }
 @Override
 protected void trainHazelcastInstance(HazelcastInstance hazelcastInstance) {
   when(hazelcastInstance.getAtomicLong("foo")).thenReturn(atomicNumber);
 }
Ejemplo n.º 6
0
 public IAtomicLong getAtomicNumber() {
   atomicNumber = hazelcast.getAtomicLong(namespace);
   return atomicNumber;
 }