All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.fabric.netty.handlers.NettyChannelAttributes Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.fabric.netty.handlers;

import com.liferay.petra.concurrent.AsyncBroker;
import com.liferay.petra.concurrent.FutureListener;
import com.liferay.petra.concurrent.NoticeableFuture;
import com.liferay.portal.fabric.netty.agent.NettyFabricAgentStub;
import com.liferay.portal.fabric.netty.rpc.RPCUtil;
import com.liferay.portal.fabric.worker.FabricWorker;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

import java.io.Serializable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Shuyang Zhou
 */
public class NettyChannelAttributes {

	public static  AsyncBroker getAsyncBroker(
		Channel channel) {

		Attribute> attribute = channel.attr(
			_asyncBrokerKey);

		AsyncBroker asyncBroker = attribute.get();

		if (asyncBroker == null) {
			asyncBroker = new AsyncBroker<>();

			AsyncBroker previousAsyncBroker =
				attribute.setIfAbsent(asyncBroker);

			if (previousAsyncBroker != null) {
				asyncBroker = previousAsyncBroker;
			}
		}

		return (AsyncBroker)asyncBroker;
	}

	public static  FabricWorker getFabricWorker(
		Channel channel, long id) {

		Map> fabricWorkers = getFabricWorkers(channel);

		if (fabricWorkers == null) {
			return null;
		}

		return (FabricWorker)fabricWorkers.get(id);
	}

	public static Map> getFabricWorkers(Channel channel) {
		Attribute>> attribute = channel.attr(
			_fabricWorkersKey);

		return attribute.get();
	}

	public static NettyFabricAgentStub getNettyFabricAgentStub(
		Channel channel) {

		Attribute attribute = channel.attr(
			_nettyFabricAgentStubKey);

		return attribute.get();
	}

	public static long nextId(Channel channel) {
		Attribute attribute = channel.attr(_idGeneratorKey);

		AtomicLong attachmentIdGenerator = attribute.get();

		if (attachmentIdGenerator == null) {
			attachmentIdGenerator = new AtomicLong();

			AtomicLong previousAttachmentIdGenerator = attribute.setIfAbsent(
				attachmentIdGenerator);

			if (previousAttachmentIdGenerator != null) {
				attachmentIdGenerator = previousAttachmentIdGenerator;
			}
		}

		return attachmentIdGenerator.getAndIncrement();
	}

	public static  void putFabricWorker(
		Channel channel, final long id, FabricWorker fabricWorker) {

		Attribute>> attribute = channel.attr(
			_fabricWorkersKey);

		Map> fabricWorkers = attribute.get();

		if (fabricWorkers == null) {
			fabricWorkers = new ConcurrentHashMap<>();

			Map> previousFabricWorkers =
				attribute.setIfAbsent(fabricWorkers);

			if (previousFabricWorkers != null) {
				fabricWorkers = previousFabricWorkers;
			}
		}

		fabricWorkers.put(id, fabricWorker);

		NoticeableFuture noticeableFuture =
			fabricWorker.getProcessNoticeableFuture();

		final Map> fabricWorkersRef = fabricWorkers;

		noticeableFuture.addFutureListener(
			new FutureListener() {

				@Override
				public void complete(Future future) {
					fabricWorkersRef.remove(id);
				}

			});
	}

	public static void setNettyFabricAgentStub(
		Channel channel, NettyFabricAgentStub nettyFabricAgentStub) {

		Attribute attribute = channel.attr(
			_nettyFabricAgentStubKey);

		attribute.set(nettyFabricAgentStub);
	}

	private static final AttributeKey>
		_asyncBrokerKey = AttributeKey.valueOf(
			RPCUtil.class.getName() + "-AsyncBroker");
	private static final AttributeKey>>
		_fabricWorkersKey = AttributeKey.valueOf(
			NettyChannelAttributes.class.getName() + "-FabricWorkers");
	private static final AttributeKey _idGeneratorKey =
		AttributeKey.valueOf(RPCUtil.class.getName() + "-IdGenerator");
	private static final AttributeKey
		_nettyFabricAgentStubKey = AttributeKey.valueOf(
			NettyFabricAgentStub.class.getName());

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy