本文整理汇总了Java中javax.jms.TopicPublisher类的典型用法代码示例。如果您正苦于以下问题:Java TopicPublisher类的具体用法?Java TopicPublisher怎么用?Java TopicPublisher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TopicPublisher类属于javax.jms包,在下文中一共展示了TopicPublisher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetTopic
import javax.jms.TopicPublisher; //导入依赖的package包/类
@Test
public void testGetTopic() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
TopicPublisher publisher = session.createPublisher(topic);
assertNotNull(publisher.getTopic());
assertSame(topic, publisher.getTopic());
publisher.close();
try {
publisher.getTopic();
fail("Cannot read topic on closed publisher");
} catch (IllegalStateException ise) {}
}
示例2: testSenderAndPublisherDest
import javax.jms.TopicPublisher; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSenderAndPublisherDest() throws Exception {
JmsPoolXAConnectionFactory pcf = new JmsPoolXAConnectionFactory();
pcf.setConnectionFactory(new ActiveMQXAConnectionFactory(
"vm://test?broker.persistent=false&broker.useJmx=false"));
QueueConnection connection = pcf.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(session.createQueue("AA"));
assertNotNull(sender.getQueue().getQueueName());
connection.close();
TopicConnection topicConnection = pcf.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher topicPublisher = topicSession.createPublisher(topicSession.createTopic("AA"));
assertNotNull(topicPublisher.getTopic().getTopicName());
topicConnection.close();
pcf.stop();
}
示例3: publish
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* @param topicConnection
* @param chatTopic
* @param userId
* @throws JMSException
* @throws IOException
*/
void publish(TopicConnection topicConnection, Topic chatTopic, String userId)
throws JMSException, IOException {
TopicSession tsession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
TopicPublisher topicPublisher = tsession.createPublisher(chatTopic);
topicConnection.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String msgToSend = reader.readLine();
if (msgToSend.equalsIgnoreCase("exit")) {
topicConnection.close();
System.exit(0);
} else {
TextMessage msg = (TextMessage) tsession.createTextMessage();
msg.setText("\n["+userId + " : " + msgToSend+"]");
topicPublisher.publish(msg);
}
}
}
示例4: send
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Sends the given {@code events} to the configured JMS Topic. It takes the current Unit of Work
* into account when available. Otherwise, it simply publishes directly.
*
* @param events the events to publish on the JMS Message Broker
*/
protected void send(List<? extends EventMessage<?>> events) {
try (TopicConnection topicConnection = connectionFactory.createTopicConnection()) {
int ackMode = isTransacted ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE;
TopicSession topicSession = topicConnection.createTopicSession(isTransacted, ackMode);
try (TopicPublisher publisher = topicSession.createPublisher(topic)) {
for (EventMessage event : events) {
Message jmsMessage = messageConverter.createJmsMessage(event, topicSession);
doSendMessage(publisher, jmsMessage);
}
} finally {
handleTransaction(topicSession);
}
} catch (JMSException ex) {
throw new EventPublicationFailedException(
"Unable to establish TopicConnection to JMS message broker.", ex);
}
}
示例5: setUp
import javax.jms.TopicPublisher; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
eventBus = new SimpleEventBus();
cut = new JmsPublisher(eventBus);
connectionFactory = mock(TopicConnectionFactory.class);
publisher = mock(TopicPublisher.class);
topic = mock(Topic.class);
converter = mock(JmsMessageConverter.class);
cut.setConnectionFactory(connectionFactory);
cut.setTopic(topic);
cut.setTransacted(true);
cut.setMessageConverter(converter);
cut.setPersistent(false);
cut.postConstruct();
cut.start();
}
示例6: closeProducer
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Close a JMS {@link MessageProducer}.
* @param messageProducer JMS Message Producer that needs to be closed.
* @throws JMSException if an error occurs while closing the producer.
*/
public void closeProducer(MessageProducer messageProducer) throws JMSException {
if (messageProducer != null) {
if (logger.isDebugEnabled()) {
logger.debug("Closing a JMS Message Producer of: " + this.connectionFactoryString);
}
if ((JMSConstants.JMS_SPEC_VERSION_1_1.equals(jmsSpec)) || (JMSConstants.JMS_SPEC_VERSION_2_0
.equals(jmsSpec))) {
messageProducer.close();
} else {
if (JMSConstants.JMSDestinationType.QUEUE.equals(this.destinationType)) {
((QueueSender) messageProducer).close();
} else {
((TopicPublisher) messageProducer).close();
}
}
}
}
示例7: testSendAndReceiveOnTopic
import javax.jms.TopicPublisher; //导入依赖的package包/类
@Test(timeout = 60000)
public void testSendAndReceiveOnTopic() throws Exception {
Connection connection = createConnection("myClientId");
try {
TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(getTopicName());
TopicSubscriber consumer = session.createSubscriber(topic);
TopicPublisher producer = session.createPublisher(topic);
TextMessage message = session.createTextMessage("test-message");
producer.send(message);
producer.close();
connection.start();
message = (TextMessage) consumer.receive(1000);
assertNotNull(message);
assertNotNull(message.getText());
assertEquals("test-message", message.getText());
} finally {
connection.close();
}
}
示例8: testPersistentMessagesForTopicDropped
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Topics shouldn't hold on to messages if there are no subscribers
*/
@Test
public void testPersistentMessagesForTopicDropped() throws Exception {
TopicConnection topicConn = createTopicConnection();
TopicSession sess = topicConn.createTopicSession(true, 0);
TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1);
pub.setDeliveryMode(DeliveryMode.PERSISTENT);
Message m = sess.createTextMessage("testing123");
pub.publish(m);
sess.commit();
topicConn.close();
checkEmpty(ActiveMQServerTestCase.topic1);
}
示例9: testPersistentMessagesForTopicDropped2
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Topics shouldn't hold on to messages when the non-durable subscribers close
*/
@Test
public void testPersistentMessagesForTopicDropped2() throws Exception {
TopicConnection topicConn = createTopicConnection();
topicConn.start();
TopicSession sess = topicConn.createTopicSession(true, 0);
TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1);
TopicSubscriber sub = sess.createSubscriber(ActiveMQServerTestCase.topic1);
pub.setDeliveryMode(DeliveryMode.PERSISTENT);
Message m = sess.createTextMessage("testing123");
pub.publish(m);
sess.commit();
// receive but rollback
TextMessage m2 = (TextMessage) sub.receive(3000);
ProxyAssertSupport.assertNotNull(m2);
ProxyAssertSupport.assertEquals("testing123", m2.getText());
sess.rollback();
topicConn.close();
checkEmpty(ActiveMQServerTestCase.topic1);
}
示例10: publish
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Publish message
*
* @param message The message
* @throws JMSException Thrown if an error occurs
*/
@Override
public void publish(final Message message) throws JMSException {
session.lock();
try {
if (ActiveMQRATopicPublisher.trace) {
ActiveMQRALogger.LOGGER.trace("send " + this + " message=" + message);
}
checkState();
((TopicPublisher) producer).publish(message);
if (ActiveMQRATopicPublisher.trace) {
ActiveMQRALogger.LOGGER.trace("sent " + this + " result=" + message);
}
} finally {
session.unlock();
}
}
示例11: createPublisher
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Create a topic publisher
*
* @param topic The topic
* @return The publisher
* @throws JMSException Thrown if an error occurs
*/
@Override
public TopicPublisher createPublisher(final Topic topic) throws JMSException {
lock();
try {
TopicSession session = getTopicSessionInternal();
if (ActiveMQRASession.trace) {
ActiveMQRALogger.LOGGER.trace("createPublisher " + session + " topic=" + topic);
}
TopicPublisher result = session.createPublisher(topic);
result = new ActiveMQRATopicPublisher(result, this);
if (ActiveMQRASession.trace) {
ActiveMQRALogger.LOGGER.trace("createdPublisher " + session + " publisher=" + result);
}
addProducer(result);
return result;
} finally {
unlock();
}
}
示例12: sendResponse
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Overrides the superclass method to use the JMS 1.0.2 API to send a response.
* <p>Uses the JMS pub-sub API if the given destination is a topic,
* else uses the JMS queue API.
*/
protected void sendResponse(Session session, Destination destination, Message response) throws JMSException {
MessageProducer producer = null;
try {
if (destination instanceof Topic) {
producer = ((TopicSession) session).createPublisher((Topic) destination);
postProcessProducer(producer, response);
((TopicPublisher) producer).publish(response);
}
else {
producer = ((QueueSession) session).createSender((Queue) destination);
postProcessProducer(producer, response);
((QueueSender) producer).send(response);
}
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
示例13: doSend
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* This implementation overrides the superclass method to use JMS 1.0.2 API.
*/
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (isPubSubDomain()) {
if (isExplicitQosEnabled()) {
((TopicPublisher) producer).publish(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
((TopicPublisher) producer).publish(message);
}
}
else {
if (isExplicitQosEnabled()) {
((QueueSender) producer).send(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
((QueueSender) producer).send(message);
}
}
}
示例14: testTopicProducerCallback
import javax.jms.TopicPublisher; //导入依赖的package包/类
/**
* Test the execute(ProducerCallback) using a topic.
*/
@Test
public void testTopicProducerCallback() throws Exception {
JmsTemplate102 template = createTemplate();
template.setPubSubDomain(true);
template.setConnectionFactory(topicConnectionFactory);
template.afterPropertiesSet();
TopicPublisher topicPublisher = mock(TopicPublisher.class);
given(topicSession.createPublisher(null)).willReturn(topicPublisher);
given(topicPublisher.getPriority()).willReturn(4);
template.execute(new ProducerCallback() {
@Override
public Object doInJms(Session session, MessageProducer producer) throws JMSException {
session.getTransacted();
producer.getPriority();
return null;
}
});
verify(topicPublisher).close();
verify(topicSession).close();
verify(topicConnection).close();
}
示例15: createPublisher
import javax.jms.TopicPublisher; //导入依赖的package包/类
@Override
public TopicPublisher createPublisher(Topic topic) throws JMSException
{
externalAccessLock.readLock().lock();
try
{
checkNotClosed();
LocalTopicPublisher publisher = new LocalTopicPublisher(this,topic,idProvider.createID());
registerProducer(publisher);
return publisher;
}
finally
{
externalAccessLock.readLock().unlock();
}
}