本文整理汇总了Java中io.netty.util.Attribute.setIfAbsent方法的典型用法代码示例。如果您正苦于以下问题:Java Attribute.setIfAbsent方法的具体用法?Java Attribute.setIfAbsent怎么用?Java Attribute.setIfAbsent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.util.Attribute
的用法示例。
在下文中一共展示了Attribute.setIfAbsent方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
Attribute<ObjectOutputStream> oosAttr = ctx.attr(OOS);
ObjectOutputStream oos = oosAttr.get();
if (oos == null) {
oos = newObjectOutputStream(new ByteBufOutputStream(out));
ObjectOutputStream newOos = oosAttr.setIfAbsent(oos);
if (newOos != null) {
oos = newOos;
}
}
synchronized (oos) {
if (resetInterval != 0) {
// Resetting will prevent OOM on the receiving side.
writtenObjects ++;
if (writtenObjects % resetInterval == 0) {
oos.reset();
}
}
oos.writeObject(msg);
oos.flush();
}
}
示例2: testGetSetString
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Test
public void testGetSetString() {
AttributeKey<String> key = AttributeKey.valueOf("Nothing");
Attribute<String> one = map.attr(key);
assertSame(one, map.attr(key));
one.setIfAbsent("Whoohoo");
assertSame("Whoohoo", one.get());
one.setIfAbsent("What");
assertNotSame("What", one.get());
one.remove();
assertNull(one.get());
}
示例3: testGetSetInt
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Test
public void testGetSetInt() {
AttributeKey<Integer> key = AttributeKey.valueOf("Nada");
Attribute<Integer> one = map.attr(key);
assertSame(one, map.attr(key));
one.setIfAbsent(3653);
assertEquals(Integer.valueOf(3653), one.get());
one.setIfAbsent(1);
assertNotSame(1, one.get());
one.remove();
assertNull(one.get());
}
示例4: getOrCreateQueryStringDecoder
import io.netty.util.Attribute; //导入方法依赖的package包/类
private QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request) {
if (null == request) {
throw new NullPointerException("Request can not be null.");
}
String uri = request.getUri();
if (null == uri) {
return null;
}
Attribute<QueryStringDecoder> queryDecoderAttr = channel.attr(queryDecoderKey);
QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();
if (null == _queryStringDecoder) {
_queryStringDecoder = new QueryStringDecoder(uri);
queryDecoderAttr.setIfAbsent(_queryStringDecoder);
}
return _queryStringDecoder;
}
示例5: channelActive
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Attribute<RequestRecorder> attr = ctx.channel().attr(DovakinConstants.RECORDER_ATTRIBUTE_KEY);
RequestRecorder var1 = attr.get();
if(var1 == null){
RequestRecorder var2 = new RequestRecorder();
var1 = attr.setIfAbsent(var2);
}
ctx.fireChannelActive();
}
示例6: getWriter
import io.netty.util.Attribute; //导入方法依赖的package包/类
private static HighPerformanceChannelWriter getWriter(Channel channel) {
Attribute<HighPerformanceChannelWriter> attr = channel.attr(ChannelAttrKeys.highPerformanceWriter);
HighPerformanceChannelWriter writer = attr.get();
if (null == writer) {
HighPerformanceChannelWriter old = attr.setIfAbsent(writer = new HighPerformanceChannelWriter(channel));
if (null != old) {
writer = old;
}
}
return writer;
}
示例7: channelRead
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Attribute<AtomicInteger> attr = ctx.attr(REQUEST_COUNT);
if (attr.get() == null) {
attr.setIfAbsent(new AtomicInteger());
}
attr.get().incrementAndGet();
ctx.fireChannelRead(msg);
}
示例8: getPingEnabled
import io.netty.util.Attribute; //导入方法依赖的package包/类
public static boolean getPingEnabled(Channel ch) {
final Attribute<Boolean> attr = ch.attr(SEND_PINGS);
attr.setIfAbsent(false);
return attr.get();
}
示例9: handle
import io.netty.util.Attribute; //导入方法依赖的package包/类
@Override
public void handle(NetworkContext context, MessagePlayInChatMessage message) {
final NetworkSession session = context.getSession();
final LanternPlayer player = session.getPlayer();
player.resetIdleTimeoutCounter();
final String message0 = message.getMessage();
// Check for a valid click action callback
final Matcher matcher = LanternClickActionCallbacks.COMMAND_PATTERN.matcher(message0);
if (matcher.matches()) {
final UUID uniqueId = UUID.fromString(matcher.group(1));
final Optional<Consumer<CommandSource>> callback = LanternClickActionCallbacks.get().getCallbackForUUID(uniqueId);
if (callback.isPresent()) {
callback.get().accept(player);
} else {
player.sendMessage(error(t("The callback you provided was not valid. Keep in mind that callbacks will expire "
+ "after 10 minutes, so you might want to consider clicking faster next time!")));
}
return;
}
String message1 = StringUtils.normalizeSpace(message0);
if (!isAllowedString(message0)) {
session.disconnect(t("multiplayer.disconnect.illegal_characters"));
return;
}
if (message1.startsWith("/")) {
Lantern.getSyncExecutorService().submit(() -> Sponge.getCommandManager().process(player, message1.substring(1)));
} else {
final Text nameText = player.get(Keys.DISPLAY_NAME).get();
final Text rawMessageText = Text.of(message0);
final GlobalConfig.Chat.Urls urls = Lantern.getGame().getGlobalConfig().getChat().getUrls();
final Text messageText;
if (urls.isEnabled() && player.hasPermission(Permissions.Chat.FORMAT_URLS)) {
messageText = newTextWithLinks(message0, urls.getTemplate(), false);
} else {
messageText = rawMessageText;
}
final MessageChannel channel = player.getMessageChannel();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(EventContextKeys.PLAYER, player);
final MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(causeStack.getCurrentCause(),
channel, Optional.of(channel), new MessageEvent.MessageFormatter(nameText, messageText), rawMessageText, false);
if (!Sponge.getEventManager().post(event) && !event.isMessageCancelled()) {
event.getChannel().ifPresent(c -> c.send(player, event.getMessage(), ChatTypes.CHAT));
}
}
}
final Attribute<ChatData> attr = context.getChannel().attr(CHAT_DATA);
ChatData chatData = attr.get();
if (chatData == null) {
chatData = new ChatData();
final ChatData chatData1 = attr.setIfAbsent(chatData);
if (chatData1 != null) {
chatData = chatData1;
}
}
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (chatData) {
final long currentTime = LanternGame.currentTimeTicks();
if (chatData.lastChatTime != -1L) {
chatData.chatThrottle = (int) Math.max(0, chatData.chatThrottle - (currentTime - chatData.lastChatTime));
}
chatData.lastChatTime = currentTime;
chatData.chatThrottle += 20;
if (chatData.chatThrottle > Lantern.getGame().getGlobalConfig().getChatSpamThreshold()) {
session.disconnect(t("disconnect.spam"));
}
}
}