Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ class ChannelInitializer implements ConnectionObserver {
@Override
public void connectionEstablished() {
outboundMessageSender.initialiseOutboundChannel(transport.getWritableByteChannel());
channelReaderExecutorService.execute(new Runnable() {
@Override
public void run() {
final ReadableByteChannel readableByteChannel = transport.getReadableByteChannel();
try {
inputStreamReader.blockingStart(readableByteChannel);
} catch (RuntimeException e) {
LOGGER.error("Exception thrown while reading from stream, channel: " + readableByteChannel, e);
}
channelReaderExecutorService.execute(() -> {
final ReadableByteChannel readableByteChannel = transport.getReadableByteChannel();
try {
inputStreamReader.blockingStart(readableByteChannel);
} catch (RuntimeException e) {
LOGGER.error("Exception thrown while reading from stream, channel: {}", readableByteChannel, e);
}
});
countDownLatch.countDown();
Expand Down
6 changes: 3 additions & 3 deletions nanofix-client/src/main/java/com/lmax/nanofix/FixClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void send(final String message) {
}

/**
* Sends a array of bytes string.
* Sends an array of bytes string.
*
* @param bytes a FIX messages.
*/
Expand All @@ -82,7 +82,7 @@ public void send(final byte[] bytes) {
}

/**
* Initiates a TCP connection with the remote host specified on construction..
* Initiates a TCP connection with the remote host specified on construction.
*/
public void connect() {
transportOps.connect();
Expand All @@ -94,7 +94,7 @@ public void connect() {
}

/**
* Initiates a TCP connection with the remote host specified on construction..
* Initiates a TCP connection with the remote host specified on construction.
*
* @param timeout the time to wait for a connection to be established
* @param units the {@link TimeUnit unit} of the timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static FixClientConfiguration createInitiatingFixClient(final String host
}

/**
* Listening listening fix client that will listen for inbound tcp connections on port
* Listening fix client that will listen for inbound tcp connections on port
*
* @param port tcp port number int between 0 and 65535
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@
public final class FixClientFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(FixClientFactory.class);

static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
LOGGER.error("Uncaught Exception thrown in thread: " + thread.getName(), throwable);
}
};
static final Thread.UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = (thread, throwable) ->
LOGGER.error("Uncaught Exception thrown in thread: {}", thread.getName(), throwable);

private FixClientFactory() {
}
Expand All @@ -68,7 +64,7 @@ public static FixClient createFixClient(final String host, final int port) {
}

/**
* Create an listening fix client that will listen for inbound tcp connections on port
* Create a listening fix client that will listen for inbound tcp connections on port
*
* @param port tcp port number int between 0 and 65535
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import com.lmax.nanofix.outgoing.OutboundMessageHandler;

public class FixSession {
private int outboundSequenceNumber;
private OutboundMessageHandler outboundMessageSender;
private final OutboundMessageHandler outboundMessageSender;

public FixSession(final OutboundMessageHandler outboundMessageSender) {
this.outboundMessageSender = outboundMessageSender;
this.outboundSequenceNumber = 1;
}

public void send(final Collection<FixMessage> messages) {
Expand Down
4 changes: 2 additions & 2 deletions nanofix-client/src/main/java/com/lmax/nanofix/FixUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static Charset getCharset() {

public static byte[] newMessage(String... tags) {
StringBuilder sb = new StringBuilder();
for (int i = 0, n = tags.length; i < n; i++) {
sb.append(tags[i]);
for (final String tag : tags) {
sb.append(tag);
sb.append(FixTagParser.SOH);
}
return sb.toString().getBytes(FixUtil.getCharset());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NamedThreadFactory implements ThreadFactory {
private final String name;
private final boolean daemon;
private final AtomicInteger count = new AtomicInteger(0);
private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;

public NamedThreadFactory(final String name, boolean daemon, Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean hasValue(int tagId) {
}

public void replace(int tagId, String value) {
final LinkedList<String> values = new LinkedList<String>();
final LinkedList<String> values = new LinkedList<>();
values.add(value);
multimap.replaceValues(tagId, values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.CopyOnWriteArrayList;

public class FixMessagePublisher implements FixMessageHandler {
List<FixMessageHandler> handlers = new CopyOnWriteArrayList<FixMessageHandler>();
List<FixMessageHandler> handlers = new CopyOnWriteArrayList<>();

public void subscribeToAllMessages(final FixMessageHandler fixMessageHandler) {
handlers.add(fixMessageHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package com.lmax.nanofix.incoming;

@SuppressWarnings("serial")
public final class FixParseException
extends RuntimeException {
public final class FixParseException extends RuntimeException {

public FixParseException(final String msg) {
super(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void parse(final ByteBuffer segment) {
}
} catch (final Exception ex) {
final String msg = String.format("Exception parsing segment:%n%s %n%s %n adding to %n%s %n%s",
segment.toString(),
segment,
new String(segment.array(), 0, segmentLimit, FixUtil.getCharset()),
fragmentedMessage.toString(),
new String(fragmentedMessage.array(), 0, fragmentedMessage.position(), FixUtil.getCharset()));
Expand Down Expand Up @@ -118,8 +118,8 @@ private void handleFragmentedMessage(final ByteBuffer segment) {

if (-1 != end) {
callbackMessage(fragmentedMessage.array(), begin, end);
// our fragmented message didnt need all the bytes in the incoming segment,
// so rewind the segment by the exact number of bytes we didnt use
// our fragmented message didn't need all the bytes in the incoming segment,
// so rewind the segment by the exact number of bytes we didn't use
segment.position(segment.position() - (fragmentedMessage.limit() - end));
fragmentedMessage.clear();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ private static void raiseException(final String reason,
final int equalsIndex) {
String msg = String.format("Parse Failed: %s at tagStart=%d equalsIndex=%d message=%s",
reason,
Integer.valueOf(tagStart),
Integer.valueOf(equalsIndex),
tagStart,
equalsIndex,
new String(message, offset, length, FixUtil.getCharset()).replace('\u0001', '|'));

throw new FixParseException(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public String toFixString() {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("FixMessage");
sb.append("{messageWithChecksum='").append(messageWithChecksum).append('\'');
sb.append('}');
return sb.toString();
return "FixMessage{messageWithChecksum='" + messageWithChecksum + "'}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.lmax.nanofix.outgoing;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;

import com.lmax.nanofix.fields.BusinessRejectionReason;
Expand Down Expand Up @@ -269,7 +269,7 @@ private String checksum(final String bodyWithLength) {
return checksumOverride;
}

return String.format("%03d", checksum(bodyWithLength.getBytes(Charset.forName("UTF-8"))));
return String.format("%03d", checksum(bodyWithLength.getBytes(StandardCharsets.UTF_8)));
}

private int checksum(final byte[] bytes) {
Expand All @@ -284,7 +284,6 @@ private String messageLength(final String body) {
if (messageLengthOverride != null) {
return messageLengthOverride;
}

return String.valueOf(body.length());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,41 @@ public DelegatingServerSocketChannel bind(final InetSocketAddress socketAddress)

@Override
public void createSocketOnIncomingConnection(final DelegatingServerSocketChannel serverSocketChannel, final SocketEstablishedCallback socketEstablishedCallback) {
executorService.execute(new Runnable() {
@Override
public void run() {
final SocketChannel socketChannel;
executorService.execute(() -> {
final SocketChannel socketChannel;
try {
try {
try {
socketChannel = serverSocketChannel.accept();
socketEstablishedCallback.onSocketEstablished(socketChannel);
} catch (AsynchronousCloseException e) {
LOGGER.warn("Server socket closed by another thread, while waiting to accept a new inbound connection.");
} catch (ClosedChannelException e) {
LOGGER.warn("Server socket closed, while waiting to accept a new inbound connection.", e);
} catch (IOException e) {
LOGGER.error("IOException occurred while waiting for a connection", e);
throw new GeneralRuntimeException("Failed to accept server socket channel", e);
}

} catch (RuntimeException e) {
LOGGER.error("Exception thrown: ", e);
socketChannel = serverSocketChannel.accept();
socketEstablishedCallback.onSocketEstablished(socketChannel);
} catch (AsynchronousCloseException e) {
LOGGER.warn("Server socket closed by another thread, while waiting to accept a new inbound connection.");
} catch (ClosedChannelException e) {
LOGGER.warn("Server socket closed, while waiting to accept a new inbound connection.", e);
} catch (IOException e) {
LOGGER.error("IOException occurred while waiting for a connection", e);
throw new GeneralRuntimeException("Failed to accept server socket channel", e);
}

} catch (RuntimeException e) {
LOGGER.error("Exception thrown: ", e);
}
});
}

@Override
public void createSocketOnOutgoingConnection(final InetSocketAddress socketAddress, final SocketEstablishedCallback socketEstablishedCallback) {
executorService.execute(new Runnable() {
@Override
public void run() {
executorService.execute(() -> {
try {
final SocketChannel socketChannel;
try {
final SocketChannel socketChannel;
try {
socketChannel = SocketChannel.open(socketAddress);
socketEstablishedCallback.onSocketEstablished(socketChannel);
} catch (IOException e) {
throw new RuntimeException(String.format("Can't connect - host:%s, port:%d", socketAddress.getHostName(), socketAddress.getPort()), e);
}
} catch (RuntimeException e) {
LOGGER.error("Caught Exception while executing", e);
socketChannel = SocketChannel.open(socketAddress);
socketEstablishedCallback.onSocketEstablished(socketChannel);
} catch (IOException e) {
throw new RuntimeException(String.format("Can't connect - host:%s, port:%d", socketAddress.getHostName(), socketAddress.getPort()), e);
}
} catch (RuntimeException e) {
LOGGER.error("Caught Exception while executing", e);
}

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class PublishingConnectionObserver implements ConnectionObserver {

private final Set<ConnectionObserver> observers = new CopyOnWriteArraySet<ConnectionObserver>();
private final Set<ConnectionObserver> observers = new CopyOnWriteArraySet<>();

public void addObserver(final ConnectionObserver connectionObserver) {
observers.add(connectionObserver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class TcpTransport implements Transport, ConnectionObserver {
private volatile SocketChannel socketChannel;
private volatile DelegatingServerSocketChannel serverSocketChannel;

private InetSocketAddress socketAddress;
private SocketFactory asyncTcpSocketFactory;
private final InetSocketAddress socketAddress;
private final SocketFactory asyncTcpSocketFactory;

public TcpTransport(final PublishingConnectionObserver publishingTransportObserver, final InetSocketAddress socketAddress,
final SocketFactory asyncTcpSocketFactory, final TransportConfig transportConfig) {
Expand All @@ -50,14 +50,10 @@ public TcpTransport(final PublishingConnectionObserver publishingTransportObserv

@Override
public void connect() {
asyncTcpSocketFactory.createSocketOnOutgoingConnection(socketAddress, new SocketFactory.SocketEstablishedCallback() {
@Override
public void onSocketEstablished(final SocketChannel socketChannel) {
TcpTransport.this.socketChannel = socketChannel;
publishingTransportObserver.connectionEstablished();
}
asyncTcpSocketFactory.createSocketOnOutgoingConnection(socketAddress, socketChannel -> {
TcpTransport.this.socketChannel = socketChannel;
publishingTransportObserver.connectionEstablished();
});

}

@Override
Expand Down Expand Up @@ -147,12 +143,9 @@ public void connectionClosed() {
}

private void acceptNewConnection() {
asyncTcpSocketFactory.createSocketOnIncomingConnection(serverSocketChannel, new SocketFactory.SocketEstablishedCallback() {
@Override
public void onSocketEstablished(final SocketChannel socketChannel) {
TcpTransport.this.socketChannel = socketChannel;
publishingTransportObserver.connectionEstablished();
}
asyncTcpSocketFactory.createSocketOnIncomingConnection(serverSocketChannel, socketChannel -> {
TcpTransport.this.socketChannel = socketChannel;
publishingTransportObserver.connectionEstablished();
});
}
}
Loading
Loading