-
Notifications
You must be signed in to change notification settings - Fork 64
/
RabbitMQChannelFactory.java
50 lines (40 loc) · 1.55 KB
/
RabbitMQChannelFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gojek.rabbitmq.channel_pool;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RabbitMQChannelFactory extends BasePooledObjectFactory<Channel> {
Logger log = LoggerFactory.getLogger(this.getClass());
private final Connection connection;
public RabbitMQChannelFactory(Connection connection) {
this.connection = connection;
}
@Override
public Channel create() throws Exception {
Channel channel = connection.createChannel();
log.info("Created a new channel with id: " + channel.getChannelNumber());
return channel;
}
@Override
public boolean validateObject(PooledObject<Channel> p) {
boolean open = p.getObject().isOpen();
if (!open) {
log.info("Channel is closed, invalidating channel with id " + p.getObject().getChannelNumber());
}
return open;
}
@Override
public PooledObject<Channel> wrap(Channel obj) {
return new DefaultPooledObject<>(obj);
}
@Override
public void destroyObject(PooledObject<Channel> p, DestroyMode destroyMode) throws Exception {
super.destroyObject(p, destroyMode);
log.info("Closing the channel with id: " + p.getObject().getChannelNumber());
p.getObject().close();
}
}