use of jp.ossc.nimbus.service.queue.DefaultQueueService in project nimbus by nimbus-org.
the class ClusterService method startService.
public void startService() throws Exception {
if (clusterListenerServiceNames != null) {
for (int i = 0; i < clusterListenerServiceNames.length; i++) {
listeners.add((ClusterListener) ServiceManagerFactory.getServiceObject(clusterListenerServiceNames[i]));
}
}
if (targetServiceName != null) {
DefaultClusterListenerService listener = new DefaultClusterListenerService();
listener.setTargetServiceName(targetServiceName);
listener.setClusterService(this);
listener.create();
listener.start();
listeners.add(listener);
}
uidWithOption = new GlobalUID(localAddress, (optionMap == null || optionMap.size() == 0) ? option : (Serializable) optionMap);
uidWithOption.setClient(isClient);
uid = (GlobalUID) uidWithOption.clone();
uid.setOption(null);
if (multicastGroupAddress == null && (unicastMemberAddresses == null || unicastMemberAddresses.length == 0)) {
throw new IllegalArgumentException("MulticastGroupAddress and UnicastMemberAddresses is null.");
}
eventQueue = new DefaultQueueService();
((Service) eventQueue).create();
((Service) eventQueue).start();
eventQueue.accept();
connect();
if (!isClient) {
synchronized (members) {
members.clear();
members.add(uidWithOption);
if (multicastGroupAddress == null) {
memberAddresses.add(new InetSocketAddress(uidWithOption.getAddress(), uidWithOption.getUnicastPort()));
}
}
}
eventHandler = new Daemon(new EventHandler());
eventHandler.setName("Nimbus Cluster EventHandler " + getServiceNameObject());
if (threadPriority >= 0) {
eventHandler.setPriority(threadPriority);
}
eventHandler.start();
clusterMessageReceiver = new Daemon(new MessageReceiver(socket));
clusterMessageReceiver.setName("Nimbus Cluster MessageReceiver " + getServiceNameObject());
if (threadPriority >= 0) {
clusterMessageReceiver.setPriority(threadPriority);
}
clusterMessageReceiver.start();
if (unicastSocket != null) {
unicastClusterMessageReceiver = new Daemon(new MessageReceiver(unicastSocket));
unicastClusterMessageReceiver.setName("Nimbus Cluster Unicast MessageReceiver " + getServiceNameObject());
if (threadPriority >= 0) {
unicastClusterMessageReceiver.setPriority(threadPriority);
}
unicastClusterMessageReceiver.start();
}
heartBeater = new Daemon(new HeartBeater());
if (threadPriority >= 0) {
heartBeater.setPriority(threadPriority);
}
heartBeater.setName("Nimbus Cluster HeartBeater " + getServiceNameObject());
heartBeater.suspend();
heartBeater.start();
if (isJoinOnStart) {
join();
}
}
use of jp.ossc.nimbus.service.queue.DefaultQueueService in project nimbus by nimbus-org.
the class ClientConnectionImpl method connect.
private synchronized void connect(Object id, boolean isReconnect) throws ConnectException {
if (socket != null) {
return;
}
isConnected = false;
try {
try {
if (socketFactory == null) {
socket = new Socket(address, port, getBindAddress(), getBindPort());
} else {
socket = socketFactory.createSocket(address, port, getBindAddress(), getBindPort());
}
if (responseTimeout > 0) {
socket.setSoTimeout((int) responseTimeout);
}
if (!isReconnect) {
if (receiveAddress != null) {
receiveGroup = InetAddress.getByName(receiveAddress);
InetAddress bindAddress = getUDPBindAddress();
if (bindAddress == null) {
receiveSocket = receiveGroup.isMulticastAddress() ? new MulticastSocket(receivePort) : new DatagramSocket(receivePort);
} else {
final InetSocketAddress address = new InetSocketAddress(bindAddress, receivePort);
receiveSocket = receiveGroup.isMulticastAddress() ? new MulticastSocket(address) : new DatagramSocket(address);
}
if (receiveGroup.isMulticastAddress()) {
NetworkInterface[] networkInterfaces = getNetworkInterfaces();
if (networkInterfaces == null) {
((MulticastSocket) receiveSocket).joinGroup(receiveGroup);
} else {
for (int i = 0; i < networkInterfaces.length; i++) {
((MulticastSocket) receiveSocket).joinGroup(new InetSocketAddress(receiveGroup, receivePort), networkInterfaces[i]);
}
}
}
} else {
InetAddress bindAddress = getUDPBindAddress();
if (bindAddress == null) {
receiveSocket = new DatagramSocket(receivePort);
} else {
receiveSocket = new DatagramSocket(new InetSocketAddress(bindAddress, receivePort));
}
}
if (receivePort == 0) {
receivePortReal = receiveSocket.getLocalPort();
} else {
receivePortReal = receivePort;
}
if (receiveSocket != null) {
try {
int receiveBufferSize = receiveSocket.getReceiveBufferSize();
if (receiveBufferSize < windowSize) {
receiveSocket.setReceiveBufferSize(windowSize);
}
} catch (SocketException e) {
}
}
}
} catch (UnknownHostException e) {
throw new ConnectException(e);
} catch (NumberFormatException e) {
throw new ConnectException(e);
} catch (IOException e) {
throw new ConnectException(e);
}
if (receivePacketQueue == null) {
receivePacketQueue = new DefaultQueueService();
try {
receivePacketQueue.create();
receivePacketQueue.start();
} catch (Exception e) {
throw new ConnectException(e);
}
}
if (packetReceiveDaemon == null) {
packetReceiveDaemon = new Daemon(new PacketReceiver());
packetReceiveDaemon.setDaemon(true);
packetReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection PacketReceiver " + socket.getLocalSocketAddress());
packetReceiveDaemon.start();
}
if (replyReceiveDaemon == null) {
replyReceiveDaemon = new Daemon(new ReplyReceiver());
replyReceiveDaemon.setDaemon(true);
replyReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection ReplyReceiver " + socket.getLocalSocketAddress());
replyReceiveDaemon.start();
}
if (messageReceiveDaemon == null) {
messageReceiveDaemon = new Daemon(new MessageReceiver());
messageReceiveDaemon.setDaemon(true);
messageReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection MessageReceiver " + socket.getLocalSocketAddress());
}
if (missingWindowCheckDaemon == null) {
missingWindowCheckDaemon = new Daemon(new MissingWindowChecker((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()));
missingWindowCheckDaemon.setDaemon(true);
missingWindowCheckDaemon.setName("Nimbus Publish(UDP) ClientConnection MissingWindowChecker " + socket.getLocalSocketAddress());
((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).setPacketReceiver((PacketReceiver) packetReceiveDaemon.getDaemonRunnable());
((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).setMissingWindowChecker((MissingWindowChecker) missingWindowCheckDaemon.getDaemonRunnable());
}
messageReceiveDaemon.start();
missingWindowCheckDaemon.start();
this.id = id == null ? socket.getLocalSocketAddress() : id;
try {
IdMessage message = new IdMessage(this.id);
message.setReceivePort(receivePortReal);
send(message, isAcknowledge);
} catch (IOException e) {
throw new ConnectException(e);
} catch (ClassNotFoundException e) {
throw new ConnectException(e);
}
if (serverServiceName != null) {
ServiceManager manager = ServiceManagerFactory.findManager(serviceManagerName == null ? serverServiceName.getServiceManagerName() : serviceManagerName);
if (manager != null) {
final ClientConnectionService ccs = new ClientConnectionService();
try {
String name = serverServiceName.getServiceName() + '$' + socket.getLocalSocketAddress();
name = name.replaceAll(":", "\\$");
if (!manager.isRegisteredService(name) && manager.registerService(name, ccs)) {
serviceName = ccs.getServiceNameObject();
manager.createService(ccs.getServiceName());
manager.startService(ccs.getServiceName());
}
} catch (Exception e) {
throw new ConnectException(e);
}
}
}
} catch (ConnectException e) {
if (socket != null) {
try {
socket.close();
} catch (IOException e2) {
}
socket = null;
}
if (!isReconnect && receiveSocket != null) {
receiveSocket.close();
receiveSocket = null;
}
throw e;
}
isConnected = true;
isServerClosed = false;
}
use of jp.ossc.nimbus.service.queue.DefaultQueueService in project nimbus by nimbus-org.
the class ServerConnectionImpl method initSend.
private void initSend(ServiceName sendQueueServiceName, int sendThreadSize, boolean isMulticast) throws Exception {
if (!isMulticast && sendThreadSize >= 1) {
sendQueueHandlerContainer = new QueueHandlerContainerService();
sendQueueHandlerContainer.create();
if (sendQueueServiceName == null) {
DefaultQueueService sendQueue = new DefaultQueueService();
sendQueue.create();
sendQueue.start();
sendQueueHandlerContainer.setQueueService(sendQueue);
} else {
sendQueueHandlerContainer.setQueueServiceName(sendQueueServiceName);
}
sendQueueHandlerContainer.setQueueHandlerSize(sendThreadSize);
sendQueueHandlerContainer.setQueueHandler(new SendQueueHandler());
sendQueueHandlerContainer.setIgnoreNullElement(true);
sendQueueHandlerContainer.setWaitTimeout(1000l);
sendQueueHandlerContainer.start();
sendResponseQueue = new DefaultQueueService();
try {
sendResponseQueue.create();
sendResponseQueue.start();
} catch (Exception e) {
throw new MessageSendException(e);
}
sendResponseQueue.accept();
}
}
use of jp.ossc.nimbus.service.queue.DefaultQueueService in project nimbus by nimbus-org.
the class ServerConnectionImpl method send.
public void send(Message message) throws MessageSendException {
addSendMessageCache((MessageImpl) message);
if (clients.size() == 0) {
return;
}
ClientImpl[] clientArray = (ClientImpl[]) clients.values().toArray(new ClientImpl[clients.size()]);
if (sendQueueHandlerContainer == null) {
for (int i = 0; i < clientArray.length; i++) {
if (!clientArray[i].isStartReceive() || !clientArray[i].isTargetMessage(message)) {
continue;
}
clientArray[i].send((MessageImpl) message);
}
} else {
DefaultQueueService responseQueue = new DefaultQueueService();
try {
responseQueue.create();
responseQueue.start();
} catch (Exception e) {
throw new MessageSendException(e);
}
responseQueue.accept();
for (int i = 0; i < clientArray.length; i++) {
if (!clientArray[i].isStartReceive() || !clientArray[i].isTargetMessage(message)) {
clientArray[i] = null;
continue;
}
sendQueueHandlerContainer.push(new AsynchContext(new SendRequest(clientArray[i], (MessageImpl) message), responseQueue));
}
List errorClients = new ArrayList();
for (int i = 0; i < clientArray.length; i++) {
if (clientArray[i] == null) {
continue;
}
AsynchContext asynchContext = (AsynchContext) responseQueue.get();
if (asynchContext.getThrowable() != null) {
errorClients.add(((SendRequest) asynchContext.getInput()).client);
}
}
if (errorClients.size() != 0) {
throw new MessageSendException("Send error : clients=" + errorClients + ", message=" + message);
}
}
}
use of jp.ossc.nimbus.service.queue.DefaultQueueService in project nimbus by nimbus-org.
the class ServerConnectionImpl method initSend.
private void initSend(ServiceName sendQueueServiceName, int sendThreadSize) throws Exception {
if (sendThreadSize >= 2) {
sendQueueHandlerContainer = new QueueHandlerContainerService();
sendQueueHandlerContainer.create();
if (sendQueueServiceName == null) {
DefaultQueueService sendQueue = new DefaultQueueService();
sendQueue.create();
sendQueue.start();
sendQueueHandlerContainer.setQueueService(sendQueue);
} else {
sendQueueHandlerContainer.setQueueServiceName(sendQueueServiceName);
}
sendQueueHandlerContainer.setQueueHandlerSize(sendThreadSize);
sendQueueHandlerContainer.setQueueHandler(new SendQueueHandler());
sendQueueHandlerContainer.setIgnoreNullElement(true);
sendQueueHandlerContainer.setWaitTimeout(1000l);
sendQueueHandlerContainer.start();
sendResponseQueue = new DefaultQueueService();
try {
sendResponseQueue.create();
sendResponseQueue.start();
} catch (Exception e) {
throw new MessageSendException(e);
}
sendResponseQueue.accept();
}
}
Aggregations