org.jgroups.Message.size()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(155)

本文整理了Java中org.jgroups.Message.size()方法的一些代码示例,展示了Message.size()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.size()方法的具体详情如下:
包路径:org.jgroups.Message
类名称:Message
方法名:size

Message.size介绍

[英]Returns the exact size of the marshalled message. Uses method size() of each header to compute the size, so if a Header subclass doesn't implement size() we will use an approximation. However, most relevant header subclasses have size() implemented correctly. (See org.jgroups.tests.SizeTest).

The return type is a long as this is the length of the payload ( #getLength()) plus metadata (e.g. flags, headers, source and dest addresses etc). Since the largest payload can be Integer.MAX_VALUE, adding the metadata might lead to an int overflow, that's why we use a long.
[中]返回已封送消息的确切大小。使用每个标头的方法size()来计算大小,因此如果标头子类没有实现size(),我们将使用近似值。然而,大多数相关的头子类都正确地实现了size()。(参见org.jgroups.tests.SizeTest)。
返回类型为long,因为这是有效负载的长度(#getLength())加上元数据(例如标志、头、源和目标地址等)。因为最大有效载荷可以是整数。MAX_值,添加元数据可能会导致int溢出,这就是为什么我们使用long。

代码示例

代码示例来源:origin: wildfly/wildfly

@Override
  public void send(final Message msg) throws Exception {
    Runnable async_send=() -> {
      ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)(msg.size() + 10));
      sendSingleMessage(msg, out);
    };
    thread_pool.execute(async_send);
  }
}

代码示例来源:origin: wildfly/wildfly

protected static long sizeOfAllMessages(Table<Message> buf, boolean include_headers) {
  return buf.stream().reduce(0L, (size,el) -> {
    if(el == null)
      return size;
    else
      return size + (include_headers? el.size() : el.getLength());
  }, (l,r) -> l);
}

代码示例来源:origin: wildfly/wildfly

public Object down(Message msg) {
  long size=msg.size();
  num_sent_msgs++;
  if(size > frag_size) {
    if(log.isTraceEnabled()) {
      StringBuilder sb=new StringBuilder("message size is ");
      sb.append(size).append(", will fragment (frag_size=").append(frag_size).append(')');
      log.trace(sb.toString());
    }
    fragment(msg, size);  // Fragment and pass down
    return null;
  }
  return down_prot.down(msg);
}

代码示例来源:origin: wildfly/wildfly

public void send(Message msg) throws Exception {
  ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)(msg.size() + 10));
  sendSingleMessage(msg, out);
}

代码示例来源:origin: wildfly/wildfly

public void send(Message msg) throws Exception {
    num_senders.incrementAndGet();
    long size=msg.size();

    lock.lock();
    try {
      if(count + size >= transport.getMaxBundleSize())
        sendBundledMessages();

      addMessage(msg, size);

      // at this point, we haven't sent our message yet !
      if(num_senders.decrementAndGet() == 0) // no other sender threads present at this time
        sendBundledMessages();
      // else there are other sender threads waiting, so our message will be sent by a different thread
    }
    finally {
      lock.unlock();
    }
  }
}

代码示例来源:origin: wildfly/wildfly

public void send(Message msg) throws Exception {
  if(msg == null)
    throw new IllegalArgumentException("message must not be null");
  num_threads.incrementAndGet();
  int tmp_write_index=getWriteIndex(read_index.get());
  // System.out.printf("[%d] tmp_write_index=%d\n", Thread.currentThread().getId(), tmp_write_index);
  if(tmp_write_index == -1) {
    log.warn("buf is full: %s\n", toString());
    unparkIfNeeded(0);
    return;
  }
  buf[tmp_write_index]=msg;
  unparkIfNeeded(msg.size());
}

代码示例来源:origin: wildfly/wildfly

public Object up(Message msg) {
  if(log.isTraceEnabled()) {
    long size=raw_buffer? msg.getLength() : msg.size();
    if(size >= min_size) {
      StringBuilder sb=new StringBuilder(local_addr + ".up(): size of message buffer=");
      sb.append(Util.printBytes(size)).append(", " + numHeaders(msg) + " headers");
      if(print_msg)
        sb.append(", headers=" + msg.printHeaders());
      log.trace(sb);
    }
  }
  return up_prot.up(msg);
}

代码示例来源:origin: wildfly/wildfly

public Object down(Message msg) {
  if(log.isTraceEnabled()) {
    long size=raw_buffer? msg.getLength() : msg.size();
    if(size >= min_size) {
      StringBuilder sb=new StringBuilder(local_addr + ".down(): size of message buffer=");
      sb.append(Util.printBytes(size)).append(", " + numHeaders(msg) + " headers");
      if(print_msg)
        sb.append(", headers=" + msg.printHeaders());
      log.trace(sb);
    }
  }
  return down_prot.down(msg);
}

代码示例来源:origin: wildfly/wildfly

protected void _send(Message msg, Address dest) {
  try {
    send(msg, dest);
  }
  catch(InterruptedIOException iex) {
  }
  catch(InterruptedException interruptedEx) {
    Thread.currentThread().interrupt(); // let someone else handle the interrupt
  }
  catch(SocketException sock_ex) {
    log.trace(Util.getMessage("SendFailure"),
         local_addr, (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders());
  }
  catch(Throwable e) {
    log.error(Util.getMessage("SendFailure"),
         local_addr, (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders());
  }
}

代码示例来源:origin: apache/geode

private void processForUnicast(Message msg, int direction) {
 Object o = msg.getHeader(unicastHeaderId);
 if (o instanceof UNICAST3.Header && stats != null) {
  UNICAST3.Header hdr = (UNICAST3.Header) o;
  switch (direction) {
   case INCOMING:
    stats.incUcastReadBytes((int) msg.size());
    break;
   case OUTGOING:
    stats.incUcastWriteBytes((int) msg.size());
    switch (hdr.type()) {
     case UNICAST3.Header.XMIT_REQ:
      stats.incUcastRetransmits();
      break;
    }
    break;
  }
 }
}

代码示例来源:origin: wildfly/wildfly

public static Buffer messageToByteBuffer(Message msg) throws Exception {
  ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size()+1);
  out.writeBoolean(msg != null);
  if(msg != null)
    msg.writeTo(out);
  return out.getBuffer();
}

代码示例来源:origin: wildfly/wildfly

protected int marshalMessagesToSameDestination(Address dest, Message[] buf, final int start_index, final int end_index,
                        int max_bundle_size) throws Exception {
  int num_msgs=0, bytes=0;
  for(int i=start_index; i != end_index; i=increment(i)) {
    Message msg=buf[i];
    if(msg != null && msg != NULL_MSG && Objects.equals(dest, msg.dest())) {
      long msg_size=msg.size();
      if(bytes + msg_size > max_bundle_size)
        break;
      bytes+=msg_size;
      num_msgs++;
      buf[i]=NULL_MSG;
      msg.writeToNoAddrs(msg.src(), output, transport.getId());
    }
  }
  return num_msgs;
}

代码示例来源:origin: wildfly/wildfly

public void run() {
  try {
    int drained=rb.drainToBlocking(remove_queue);
    if(drained == 1) {
      output.position(0);
      sendSingleMessage(remove_queue[0]);
      return;
    }
    for(int i=0; i < drained; i++) {
      Message msg=remove_queue[i];
      long size=msg.size();
      if(count + size >= transport.getMaxBundleSize())
        sendBundledMessages();
      addMessage(msg, msg.size());
    }
    sendBundledMessages();
  }
  catch(Throwable t) {
  }
}

代码示例来源:origin: wildfly/wildfly

protected int marshalMessagesToSameDestination(Address dest, Message[] buf,
                        int start_index, int available_msgs, int max_bundle_size) throws Exception {
  int num_msgs=0, bytes=0;
  while(available_msgs > 0) {
    Message msg=buf[start_index];
    if(msg != null && Objects.equals(dest, msg.dest())) {
      long msg_size=msg.size();
      if(bytes + msg_size > max_bundle_size)
        break;
      bytes+=msg_size;
      num_msgs++;
      buf[start_index]=null;
      msg.writeToNoAddrs(msg.src(), output, transport.getId());
    }
    available_msgs--;
    start_index=increment(start_index);
  }
  return num_msgs;
}

代码示例来源:origin: wildfly/wildfly

protected int marshalMessagesToSameDestination(Address dest, Message[] buf,
                        int start_index, final int end_index, int max_bundle_size) throws Exception {
  int num_msgs=0, bytes=0;
  for(;;) {
    Message msg=buf[start_index];
    if(msg != null && Objects.equals(dest, msg.dest())) {
      long size=msg.size();
      if(bytes + size > max_bundle_size)
        break;
      bytes+=size;
      num_msgs++;
      buf[start_index]=null;
      msg.writeToNoAddrs(msg.src(), output, transport.getId());
    }
    if(start_index == end_index)
      break;
    start_index=advance(start_index);
  }
  return num_msgs;
}

代码示例来源:origin: wildfly/wildfly

@Override
protected void sendMcastDiscoveryRequest(Message msg) {
  try {
    if(msg.getSrc() == null)
      msg.setSrc(local_addr);
    ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size());
    msg.writeTo(out);
    for(int i=bind_port; i <= bind_port+port_range; i++) {
      DatagramPacket packet=new DatagramPacket(out.buffer(), 0, out.position(), dest_addr, i);
      sock.send(packet);
    }
  }
  catch(Exception ex) {
    log.error(Util.getMessage("FailedSendingDiscoveryRequest"), ex);
  }
}

代码示例来源:origin: wildfly/wildfly

protected void sendSingleMessage(final Message msg) {
  Address dest=msg.getDest();
  try {
    Util.writeMessage(msg, output, dest == null);
    transport.doSend(output.buffer(), 0, output.position(), dest);
    if(transport.statsEnabled())
      transport.incrNumSingleMsgsSent(1);
  }
  catch(SocketException | SocketTimeoutException sock_ex) {
    log.trace(Util.getMessage("SendFailure"),
         transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders());
  }
  catch(Throwable e) {
    log.error(Util.getMessage("SendFailure"),
         transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders());
  }
}

代码示例来源:origin: wildfly/wildfly

protected void sendSingleMessage(final Message msg, final ByteArrayDataOutputStream output) {
  Address dest=msg.getDest();
  try {
    output.position(0);
    Util.writeMessage(msg, output, dest == null);
    transport.doSend(output.buffer(), 0, output.position(), dest);
    if(transport.statsEnabled())
      transport.incrNumSingleMsgsSent(1);
  }
  catch(SocketException | SocketTimeoutException sock_ex) {
    log.trace(Util.getMessage("SendFailure"),
         transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders());
  }
  catch(Throwable e) {
    log.error(Util.getMessage("SendFailure"),
         transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders());
  }
}

代码示例来源:origin: apache/geode

@Test
public void recorderHandlesRejectedExecution() throws Exception {
 Message msg = mock(Message.class);
 when(msg.getHeader(any(Short.class))).thenReturn(Header.createDataHeader(1L, (short) 1, true));
 when(msg.size()).thenReturn(150L);
 // GEODE-1178, the TP protocol may throw a RejectedExecutionException & StatRecorder should
 // retry
 when(mockDownProtocol.down(any(Event.class))).thenThrow(new RejectedExecutionException());
 // after the first down() throws an exception we want StatRecorder to retry, so
 // we set the Manager to say no shutdown is in progress the first time and then say
 // one IS in progress so we can break out of the StatRecorder exception handling loop
 when(services.getCancelCriterion()).thenReturn(new Services().getCancelCriterion());
 Manager manager = mock(Manager.class);
 when(services.getManager()).thenReturn(manager);
 when(manager.shutdownInProgress()).thenReturn(Boolean.FALSE, Boolean.TRUE);
 verify(mockDownProtocol, never()).down(isA(Event.class));
 Event evt = new Event(Event.MSG, msg);
 recorder.down(evt);
 verify(mockDownProtocol, times(2)).down(isA(Event.class));
}

代码示例来源:origin: wildfly/wildfly

protected Object _down(Message msg) throws Exception {
  Address dest=msg.dest();
  setSourceAddress(msg); // very important !! listToBuffer() will fail with a null src address !!
  int size=(int)msg.size();
  ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(size + Global.INT_SIZE);
  out.writeInt(size);
  msg.writeTo(out);
  if(dest != null) // unicast
    sendTo(dest, out.buffer(), 0, out.position());
  else { // multicast
    Collection<Address> dests=view != null? view.getMembers(): addr_table.keySet();
    for(Address dst: dests) {
      try {
        sendTo(dst, out.buffer(), 0, out.position());
      }
      catch(Throwable t) {
        log.error("failed sending multicast message to " + dst, t);
      }
    }
  }
  return null;
}

相关文章