下面列出了怎么用com.hazelcast.core.IAtomicLong的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main( String[] args ) throws FileNotFoundException, InterruptedException
{
Config config = new Config();
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
IAtomicLong atomicLong = hazelcastInstance.getAtomicLong("soy productor");
boolean cambiado = atomicLong.compareAndSet(0, 1);
if (cambiado){
produce(hazelcastInstance);
} else {
consume(hazelcastInstance);
}
}
@Setup
public void setup() {
counters = new IAtomicLong[countersLength];
for (int i = 0; i < countersLength; i++) {
counters[i] = targetInstance.getAtomicLong("" + i);
counters[i].get();
}
}
@Override
public IAtomicLong getAtomicLong(String s) {
return null;
}
public void startServer() throws Exception {
LOG.info("starting hazelcast ...");
executorService = Executors.newSingleThreadExecutor();
// Register serializers in hazelcast configuration
SerializerConfig sc = new SerializerConfig()
.setImplementation(new InstanceInfoSerializer())
.setTypeClass(InstanceInfo.class);
Config config = new Config();
config.getSerializationConfig().addSerializerConfig(sc);
// Create hazelcast instance
hazelcastInstance = Hazelcast.newHazelcastInstance(config);
MembershipListenerImpl membershipListener = new MembershipListenerImpl(expectedClusterSize);
// listen on cluster events
Cluster cluster = hazelcastInstance.getCluster();
cluster.addMembershipListener(membershipListener);
LOG.info("Waiting for expected cluster members to join ...");
membershipListener.awaitClusterFormation(20, TimeUnit.SECONDS);
// populate clusterInfo map
IAtomicLong webPortCounter = hazelcastInstance.getAtomicLong("webPortCounter");
Map<String, InstanceInfo> clusterInfo = hazelcastInstance.getMap( "clusterInfo" );
InstanceInfo instanceInfo = createInstanceInfo(cluster.getLocalMember(), clusterInfo, (int)webPortCounter.getAndAdd(1));
clusterInfo.put(instanceInfo.getId(), instanceInfo);
// register leadership listener
GateKeepingListener gateKeepingListener = new GateKeepingListenerImpl();
GateKeeperRunnable gateKeeperRunnable = new GateKeeperRunnable(executorService, hazelcastInstance, gateKeepingListener);
executorService.submit(gateKeeperRunnable);
LOG.info("initializing service layer ...");
MessageService messageService = new MessageServiceImpl(hazelcastInstance);
RequestRouter requestRouter = new RequestRouter(messageService);
LOG.info("starting web layer ...");
server = new Server();
ServletContextHandler context = new ServletContextHandler(server, "/data", ServletContextHandler.SESSIONS);
// Register websocket handlers
ServletHolder webSocketHolder = new ServletHolder(new WsServlet(requestRouter));
context.addServlet(webSocketHolder, "/websocket");
// Setup http connectors
HttpConfiguration httpConfig = new HttpConfiguration();
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfig);
ServerConnector http = new ServerConnector(server, httpConnectionFactory);
http.setPort(instanceInfo.getWebServerPort());
server.addConnector(http);
server.start();
LOG.info("init sequence done.");
}
public HazelcastAtomicLong(IAtomicLong delegate)
{
this.delegate = delegate;
}
private IAtomicLong randomCounter() {
int index = randomInt(counters.length);
return counters[index];
}
@Teardown
public void teardown() {
for (IAtomicLong counter : counters) {
counter.destroy();
}
}
public IAtomicLong getAtomicLong(String name) {
return targetInstance.getDistributedObject("hz:impl:atomicLongService", name);
}