-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicPublisher.java
More file actions
47 lines (39 loc) · 1.45 KB
/
TopicPublisher.java
File metadata and controls
47 lines (39 loc) · 1.45 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.javaee7.chapter10;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.jms.JMSException;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.NamingException;
/**
*
* @author Juneau
*/
@Named("topicPublisher")
public class TopicPublisher implements java.io.Serializable {
@Resource(name = "jms/TopicConnFactory")
private TopicConnectionFactory topicConnFactory;
@Resource(lookup = "jms/Topic")
Topic topic;
public void publishMessage() throws NamingException, JMSException {
try (TopicConnection topicConnection = topicConnFactory.createTopicConnection();
TopicSession topicSession =
topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);) {
topicConnection.start();
Topic createdtopic = topicSession.createTopic("JavaEE");
// create the message to send
TextMessage textMessage = topicSession.createTextMessage("This is a test message");
javax.jms.TopicPublisher topicPublisher = topicSession.createPublisher(createdtopic);
topicPublisher.setDeliveryDelay(1000);
topicPublisher.publish(textMessage);
}
}
}