I guess netty is best java networking framework ever i know, after reading and try some sample i have question:
1. What the best way to create Network Server for multi port with different protocol using netty 4.0?
Each server create :
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap(); // (2)
Each Server Running Inside Thread
is that right way?
2. Websocket Server
How to securing Websocket Server for Cross origin case? I don't have any reference about it
Your help very appreciate,
Regards
BC,
As Norman said, the important thing is that you need to share the event loop groups so that you do not create way too many threads. As long as you share the event loop groups, you can create as many ServerBootstraps as you wish:
EventLoopGroup bossGroup = new NioEventLoopGroup(numBossThreads);
EventLoopGroup workerGroup = new NioEventLoopGroup(numWorkerThreads);
ServerBootstrap sb1 = new ServerBootstrap();
sb1.group(bossGroup, workerGroup);
...
sb1.bind();
ServerBootstrap sb2 = new ServerBootstrap();
sb2.group(bossGroup, workerGroup);
...
sb2.bind();
ServerBootstrap sb3 = new ServerBootstrap();
sb3.group(bossGroup, workerGroup);
...
sb3.bind();
The bossGroup is used to accept the incoming connections, and the workerGroup is used to handle the connections accepted by the bossGroup. Please do some performance tests and specify the optimal numBossThreads and numWorkerThreads.
I would share the NioEventLoopGroup between the ServerBootstrap to share the same threads.
Related
I have successfully created a connection between client and server(localhost) in ignite.But while trying to connect the ignite server which is running in remote IP(eg: 192.168.33.44), I am not able to establish connection. The client side configuration given below.
#Bean(name = "igniteConfiguration")
public IgniteConfiguration igniteConfiguration() {
IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
igniteConfiguration.setClientMode(true);
igniteConfiguration.setPeerClassLoadingEnabled(true);
igniteConfiguration.setLocalHost("127.0.0.1");
TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi();
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
tcpDiscoverySpi.setIpFinder(ipFinder);
tcpDiscoverySpi.setLocalPort(47500);
// Changing local port range. This is an optional action.
tcpDiscoverySpi.setLocalPortRange(9);
tcpDiscoverySpi.setLocalAddress("localhost");
igniteConfiguration.setDiscoverySpi(tcpDiscoverySpi);
TcpCommunicationSpi communicationSpi = new TcpCommunicationSpi();
communicationSpi.setLocalAddress("localhost");
communicationSpi.setLocalPort(48100);
communicationSpi.setSlowClientQueueLimit(1000);
igniteConfiguration.setCommunicationSpi(communicationSpi);
igniteConfiguration.setCacheConfiguration(cacheConfiguration());
return igniteConfiguration;
}
Can anyone help me to make code change for creating a successful client-server connecion.Thanks in advance.
Since you are moving from localhost deployment, you need to do the following changes:
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("192.168.33.44:47500..47509"));
Most likely, the server configurations need to be changed as well.
My Application uses ElastiCache on AWS for caching purposes. Our current set up uses a basic Redis Cluster with no sharding or failover. We need to now move to a Clustered Redis Elastic Cache with sharding, failover etc enabled. Creating a new cluster on AWS was the easy bit, but we are a bit lost on how to modify our java code to reads and write from the cluster.
Current Implementation -
Initialize a JedisPool.
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxWaitMillis(50);
jedisPoolConfig.setTestOnBorrow(true);
String host = "mycache.db8e1v.0001.usw2.cache.amazonaws.com";
int port = 6379;
int timeout = 50;
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout)
A Jedis object is borrowed from the pool everytime we need to perform an operation
Jedis jedis = JedisPool.getResource();
The new implementation would be
JedisPoolConfig jedisPoolConfig = ...
HostAndPort hostAndPort = new HostAndPort(host, port);
jedisCluster = new JedisCluster(Collections.singleton(hostAndPort), jedisPoolConfig);
Question:
The documentation says JedisCluster is to be used in place of Jedis (not JedisPool). Does this mean I need to create and destroy a JedisCluster object in each thread. Or can I re-use the same object and it will handle the thread safety? When do I exactly close the JedisCluster then? At the end of the application?
The JedisCluster holds internal JedisPools for each node in the cluster.
Does this mean I need to create and destroy a JedisCluster object in
each thread. Or can I re-use the same object and it will handle the
thread safety?
You can reuse the same object.
When do I exactly close the JedisCluster then? At the end of the
application?
Yes.
Replacing all Jedis-calls with JedisCluster-calls is the best way to migrate.
But I wanted pipeline support which JedisCluster currently lacks. So one other idea is to extend JedisCluster to return the JedisPool>Jedis for a particular key:
protected Jedis getJedis(String key) {
int slot = JedisClusterCRC16.getSlot(key);
return connectionHandler.getConnectionFromSlot(slot);
}
The extended class has to be in namespace redis.clients.jedis to access getConnectionFromSlot.
Now a pipeline can be executed on the Jedis.
And you need a different Jedis for each key you want to operate on. Which makes sense - in cluster mode, each key can be on a different node.
Using Red5 and
https://github.com/Red5/red5-websocket-chat
I try to do a basic chat.
It works ok for a example channel
var socket = new WebSocket('ws://serverIP:80/chat', 'chat');
Is there any way to do something similar to chat rooms using Red5 Websocket chat?
An example I want to do from JavaScript is using URL:
var socketRoom1 = new WebSocket('ws://serverIP:80/chat/Room1', 'chat');
var socketRoom2 = new WebSocket('ws://serverIP:80/chat/Room2', 'chat');
...
var socketRoomN = new WebSocket('ws://serverIP:80/chat/RoomN', 'chat');
or using Protocol:
var socketRoom1 = new WebSocket('ws://serverIP:80/chat', 'Room1');
var socketRoom2 = new WebSocket('ws://serverIP:80/chat', 'Room2');
...
var socketRoomN = new WebSocket('ws://serverIP:80/chat', 'RoomN');
But I only can make it works in JavaScript with that:
var socket = new WebSocket('ws://serverIP:80/chat', 'chat');
Thanks for your time.
You are able to do this by integrating with the Red5 scopes and creating the new scopes as needed. Scopes are basically interchangeable with "rooms" or contexts. This endeavor will require that you learn at least at a basic level how the scopes work. You'll also need to modify / extend the listener to add/remove the scopes as needed and to route your messages.
https://github.com/Red5/red5-websocket-chat/blob/master/src/main/java/org/red5/demos/chat/WebSocketChatDataListener.java
Here's some additional reading regarding scopes / rooms:
http://ria101.wordpress.com/2010/03/09/red5-cabin-fever-advanced-scope-and-room-management/
http://gregoire.org/2009/04/07/on-demand-room-scope-creation/
I need a jetty server with multiple servletHandler.
HTTPservlet:
ServletHandler servletHandler = new ServletHandler();
server.setHandler(servletHandler);
servletHandler.addServletWithMapping("com.realtime.webserver.MyServlet", "/MyServlet");
WebsocketServlet:
MyWebSocketHandler myWebSocketHandler = new MyWebSocketHandler ();
myWebSocketHandler.setHandler(new DefaultHandler());
server.setHandler(myWebSocketHandler);
server.start();
I need both should be in single server.
Is there any possibilities?
You can use org.eclipse.jetty.server.handler.HandlerCollection (Jetty 9)
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[] {servletHandler, myWebSocketHandler});
Later add handlers to the collection:
handlerCollection.addHandler(newHandler);
Finally,
server.setHandler(handlerCollection);
server.start();
http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/examples/embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java
This is an example of using many handlers at once on the same server.
Eventually it will be added to the documentation here:
http://www.eclipse.org/jetty/documentation/current/embedded-examples.html
Until that time there are many other examples there that should help make things clearer as well.
I am currently working on a single page web app that has a growing number of real-time widgets concurrently requiring different datasources. My question is which approach is best for using WebSockets as the vehicle for this data:
Option 1: The client opens a single socket connection with the server, then uses an API similar to the following to subscribe/unsubscribe/get/post data:
var socket = new WebSocket([URL]);
socket.emit("subscribe", [OPTIONS]);
socket.emit("unsubscribe", [OPTIONS]);
socket.emit("get", [OPTIONS]);
socket.emit("post", [OPTIONS])
Option 2: The client opens a socket for every source of data it needs to run the widgets on the page. Keep in mind that there could be many widgets requiring many different sources of data. For example, it would look something like this:
var sock1 = new WebSocket([URL]);
var widget1 = new Widget1(sock1);
var sock2 = new WebSocket([URL]);
var widget2 = new Widget2(sock2);
var sock3 = new WebSocket([URL]);
var widget3 = new Widget3(sock3);
Any insight would be much appreciated.
Andy