} catch(Exception ex) {
this.mes = null ;
Log.error(ex);
}
}
public static GFConcurrentUtil getInstance() {
return _instance ;
}
public ManagedExecutorService getExecutorService() throws Exception {
if(this.mes == null) {
throw new Exception("error getting glassfish managed executor service");
}
return this.mes ;
}
We make use of GFConcurrentUtil inside our rabbitmq producer class
ConnectionFactory factory = new ConnectionFactory();
...
factory.setHost(host);
factory.setUsername(user);
factory.setPassword(password);
ManagedExecutorService mes = GFConcurrentUtil.getInstance().getExecutorService() ;
Connection connection = factory.newConnection(mes);
Channel channel = connection.createChannel();
## security exception and server.policy file
The rabbitmq producer class inside GFv4.1 will produce a security exception.
That is because Rabbitmq client will try to check security permissions, This line
sm.checkPermission(new RuntimePermission("modifyThread"));
results in (Access Denied Exception)
To fix this issue, you need to add exceptions in GlassFish domain server.policy file
// rjha added for rabbitmq client
grant {
permission java.lang.RuntimePermission "modifyThread";
permission java.lang.RuntimePermission "modifyThreadGroup";
} ;
## connection and channel usage pattern
channel design in rabbitmq is per thread so it is not good to share a stateful channel between threads. we should create a channel on each web client hit.
web client -> rabbitmq producer -> new channel -> write message -> close channel
connection represents an open socket to the rabbitmq machine and can be shared between two different web client requests. The number of channels created and closed in a time period will depend on the number of web requests served. The number of connections should be such that http threads should not be waiting for a rabbitmq connection.
## verification
To verify is we are actually using managed executor service threads, we can attach JConsole to Glassfish and verify the name of threads.
## future work
Use BTrace and verify that all calls to start a new Thread are using managed executor service.
## questions
(a) can rabbitmq connections be cached and shared?
(b) what methods inside the client call executor service?
(c) does rabbitmq shutdown sequence call a managed executor service shutdown?