Skip to content

Commit baa90a6

Browse files
author
Igor Polevoy
committed
#259 Improve configuration of Async
1 parent 107f6fc commit baa90a6

File tree

1 file changed

+16
-9
lines changed
  • javalite-async/src/main/java/org/javalite/async

1 file changed

+16
-9
lines changed

javalite-async/src/main/java/org/javalite/async/Async.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class Async {
6565

6666
private final static Logger LOGGER = LoggerFactory.getLogger("JavaLite Async");
6767

68+
private final static int MIN_LARGE_MESSAGE_SIZE = 100 * 4096;
69+
6870
private static final String QUEUE_NAMESPACE = "/queue/";
6971
private Injector injector;
7072
private final Configuration config;
@@ -112,21 +114,24 @@ public Async(String dataDirectory, boolean useLibAio, Injector injector, QueueCo
112114
configureAcceptor();
113115
configureConnectionFactory();
114116
configurePaging();
115-
116117
configureQueues(queueConfigs);
117-
118-
config.setJournalType(useLibAio ? JournalType.ASYNCIO : JournalType.NIO);
118+
configureJournal(useLibAio);
119119
config.setThreadPoolMaxSize(-1);
120120
config.setScheduledThreadPoolMaxSize(10);
121-
jmsServer.setConfiguration(config);
122-
jmsServer.setJmsConfiguration(jmsConfig);
121+
123122
} catch (AsyncException e) {
124123
throw e;
125124
} catch (Exception e) {
126125
throw new AsyncException("Failed to start EmbeddedJMS", e);
127126
}
128127
}
129128

129+
private void configureJournal(boolean useLibAio){
130+
config.setJournalType(useLibAio ? JournalType.ASYNCIO : JournalType.NIO);
131+
config.setJournalBufferSize_AIO(2 * MIN_LARGE_MESSAGE_SIZE);
132+
config.setJournalBufferSize_NIO(2 * MIN_LARGE_MESSAGE_SIZE);
133+
}
134+
130135
private void configureLocations(String dataDirectory) {
131136
if (dataDirectory == null || !new File(dataDirectory).exists()) {
132137
throw new AsyncException("Must provide data directory that exists");
@@ -156,9 +161,11 @@ In my case, below configuration (made on JBoss 7.1.1.Final) has helped:
156161
<connection-ttl>-1</connection-ttl>
157162
<reconnect-attempts>-1</reconnect-attempts>
158163
*/
164+
159165
cfConfig.setClientFailureCheckPeriod(Long.MAX_VALUE);
160166
cfConfig.setConnectionTTL(-1);
161167
cfConfig.setReconnectAttempts(-1);
168+
cfConfig.setCompressLargeMessages(true);
162169
jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
163170
}
164171

@@ -212,7 +219,6 @@ private void configureListeners(Injector injector, List<QueueConfig> queueConfig
212219

213220
///******* PUBLIC METHODS BELOW ***********///
214221

215-
216222
/**
217223
* Sends a command into a queue for processing
218224
*
@@ -247,7 +253,7 @@ public void send(String queueName, Command command, int deliveryMode) {
247253
public void send(String queueName, Command command, int deliveryMode, int priority, int timeToLive) {
248254
checkStarted();
249255

250-
try {
256+
try(Session session = producerConnection.createSession()) {
251257
checkInRange(deliveryMode, 1, 2, "delivery mode");
252258
checkInRange(priority, 0, 9, "priority");
253259
if (timeToLive < 0)
@@ -257,7 +263,6 @@ public void send(String queueName, Command command, int deliveryMode, int priori
257263
if (queue == null)
258264
throw new AsyncException("Failed to find queue: " + queueName);
259265

260-
Session session = producerConnection.createSession();
261266
TextMessage msg = session.createTextMessage(command.toXml());
262267
MessageProducer p = session.createProducer(queue);
263268
p.send(msg, deliveryMode, priority, timeToLive);
@@ -268,13 +273,15 @@ public void send(String queueName, Command command, int deliveryMode, int priori
268273
}
269274
}
270275

271-
272276
/**
273277
* Starts the server.
274278
*/
275279
public void start(){
276280

277281
try {
282+
jmsServer.setConfiguration(config);
283+
jmsServer.setJmsConfiguration(jmsConfig);
284+
278285
jmsServer.start();
279286

280287
ConnectionFactory connectionFactory = (ConnectionFactory) jmsServer.lookup("/cf");

0 commit comments

Comments
 (0)