forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueUtils.java
57 lines (49 loc) · 1.89 KB
/
QueueUtils.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
51
52
53
54
55
56
57
package mock.contract;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class QueueUtils {
private static final Logger logger = LoggerFactory.getLogger(QueueUtils.class);
private static final Connection connection;
public static Connection getConnection() {
return connection;
}
static {
try {
logger.debug("waiting for activemq connection ...");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&waitForStart=10000");
connection = connectionFactory.createConnection();
connection.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void send(String queueName, String text, int delayMillis) {
new Thread(() -> {
try {
logger.info("*** artificial delay {}: {}", queueName, delayMillis);
Thread.sleep(delayMillis);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(queueName);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(text);
producer.send(message);
logger.info("*** sent message {}: {}", queueName, text);
session.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();
}
}