本文整理汇总了Java中javax.jms.MessageProducer.send方法的典型用法代码示例。如果您正苦于以下问题:Java MessageProducer.send方法的具体用法?Java MessageProducer.send怎么用?Java MessageProducer.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.MessageProducer
的用法示例。
在下文中一共展示了MessageProducer.send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doSendTextMessage
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void doSendTextMessage( final Session session, final Destination destination,
final String textMessage,
final Map<String, ?> properties ) throws JMSException {
try {
final Message message = textMessage != null
? session.createTextMessage(textMessage)
: session.createTextMessage();
if (properties != null) {
// Note: Setting any properties (including JMS fields) using
// setObjectProperty might not be supported by all providers
// Tested with: ActiveMQ
for (final Entry<String, ?> property : properties.entrySet()) {
message.setObjectProperty(property.getKey(), property.getValue());
}
}
final MessageProducer producer = session.createProducer(destination);
producer.send(message);
} finally {
releaseSession(false);
}
}
示例2: doSendBinaryMessage
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void doSendBinaryMessage( final Session session, final Destination destination,
final byte[] bytes,
final Map<String, ?> properties ) throws JMSException {
try {
BytesMessage message = session.createBytesMessage();
message.writeBytes(bytes);
if (properties != null) {
// Note: Setting any properties (including JMS fields) using
// setObjectProperty might not be supported by all providers
// Tested with: ActiveMQ
for (final Entry<String, ?> property : properties.entrySet()) {
message.setObjectProperty(property.getKey(), property.getValue());
}
}
final MessageProducer producer = session.createProducer(destination);
producer.send(message);
} finally {
releaseSession(false);
}
}
示例3: handleMessage
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void handleMessage(MessageHandler handler, Message message, Session session) {
if (! (message instanceof TextMessage)) {
return;
}
TextMessage textMessage = (TextMessage) message;
String text = null;
String requestId = UUID.randomUUID().toString();
try {
text = textMessage.getText();
if (StringUtils.isNotEmpty(message.getJMSCorrelationID())) {
requestId = message.getJMSCorrelationID();
}
MDC.put(X_OBOS_REQUEST_ID, requestId);
log.info("Received message '{}'", text);
handler.handle(new ObjectMapper().readTree(text));
} catch (Exception e) {
log.error("Failed to process message", e);
try {
TextMessage errorMessage = session.createTextMessage(text);
errorMessage.setJMSCorrelationID(requestId);
Queue queue = session.createQueue(queueError);
MessageProducer errorProducer = session.createProducer(queue);
errorProducer.send(errorMessage);
} catch (JMSException jmse) {
log.error("Failed to create error message", jmse);
}
} finally {
MDC.remove(X_OBOS_REQUEST_ID);
}
}
示例4: publiceerMaakSelectieGeenResultaatNetwerkTaak
import javax.jms.MessageProducer; //导入方法依赖的package包/类
@Override
public void publiceerMaakSelectieGeenResultaatNetwerkTaak(List<MaakSelectieResultaatTaak> maakSelectieGeenResultaatNetwerkTaken) {
LOGGER.info("publiceer maak selectie resultaat taken");
final ProducerCallback<Void> producerCallback = (final Session session, final MessageProducer producer) -> {
LOGGER.debug("publiceer maak selectie resultaat taak");
for (final MaakSelectieResultaatTaak maakSelectieGeenResultaatNetwerkTaak : maakSelectieGeenResultaatNetwerkTaken) {
final String
groupId =
maakSelectieGeenResultaatNetwerkTaak.getSelectieRunId() + "_" + maakSelectieGeenResultaatNetwerkTaak
.getToegangLeveringsAutorisatieId()
+ "_"
+ maakSelectieGeenResultaatNetwerkTaak.getDienstId();
final Message message = session.createTextMessage(serializer.serialiseerNaarString(maakSelectieGeenResultaatNetwerkTaak));
message.setStringProperty(LeveringConstanten.JMS_MESSAGEGROUP_HEADER, groupId);
producer.send(message);
}
return null;
};
PublicatieHelper.publiceer(maakSelectieGeenResultaatNetwerkTemplate, producerCallback,
() -> "fout in verzenden berichten naar maak selectie geen resultaat netwerk taak queue");
}
示例5: validateFactoryCreationWithActiveMQLibraries
import javax.jms.MessageProducer; //导入方法依赖的package包/类
/**
* This test simply validates that {@link ConnectionFactory} can be setup by
* pointing to the location of the client libraries at runtime. It uses
* ActiveMQ which is not present at the POM but instead pulled from Maven
* repo using {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which
* implies that for this test to run the computer must be connected to the
* Internet. If computer is not connected to the Internet, this test will
* quietly fail logging a message.
*/
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
try {
String libPath = TestUtils.setupActiveMqLibForTesting(true);
TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
runner.addControllerService("cfProvider", cfProvider);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI,
"vm://localhost?broker.persistent=false");
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
"org.apache.activemq.ActiveMQConnectionFactory");
runner.enableControllerService(cfProvider);
runner.assertValid(cfProvider);
Connection connection = cfProvider.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("myqueue");
MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello");
producer.send(message);
assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
connection.stop();
connection.close();
} catch (Exception e) {
logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
}
}
示例6: sendWithReplyToTemp
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void sendWithReplyToTemp(ConnectionFactory cf, String serviceQueue) throws JMSException, InterruptedException {
Connection connection = cf.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue tempQueue = session.createTemporaryQueue();
TextMessage msg = session.createTextMessage("Request");
msg.setJMSReplyTo(tempQueue);
MessageProducer producer = session.createProducer(session.createQueue(serviceQueue));
producer.send(msg);
MessageConsumer consumer = session.createConsumer(tempQueue);
Message replyMsg = consumer.receive();
assertNotNull(replyMsg);
LOG.debug("Reply message: {}", replyMsg);
consumer.close();
producer.close();
session.close();
connection.close();
}
示例7: sendObjectMsgSingleSession
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void sendObjectMsgSingleSession(List<? extends Serializable> objectsToSend) throws JMSException {
Session session = null;
Connection conn = null;
try {
conn = qFactory.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
for (Serializable objectToSend : objectsToSend) {
ObjectMessage msg = session.createObjectMessage();
msg.setObject(objectToSend);
producer.send(msg);
}
} finally {
closeSession(session);
closeConnection(conn);
}
}
示例8: sendMessages
import javax.jms.MessageProducer; //导入方法依赖的package包/类
public void sendMessages(ConnectionFactory connectionFactory) throws Exception {
for (int i = 0; i < NUM_MESSAGES; i++) {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(QUEUE);
MessageProducer producer = session.createProducer(destination);
String msgTo = "hello";
TextMessage message = session.createTextMessage(msgTo);
producer.send(message);
connection.close();
LOG.debug("sent " + i + " messages using " + connectionFactory.getClass());
}
}
示例9: sendMessage
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private static void sendMessage(Session session, MessageProducer producer) throws JMSException {
/*
for (int i = 0; i < SEND_NUMBER; i++) {
TextMessage message = session.createTextMessage("ActiveMQ Send Message:"+i);
System.out.println("SendMessage:"+""+i);
// 发送消息到目的地方
producer.send(message);
}//*/
int i = 0;
do{
TextMessage message = session.createTextMessage("ActiveMQ Send Message:"+i);
System.out.println("SendMessage:"+""+i);
// 发送消息到目的地方
producer.send(message);
i++;
if(i > SEND_NUMBER)
break;
}while(true);
}
示例10: testReception
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void testReception(
final TestClass i,
final Destination destination, final ThrowingConsumer<Destination> destinationCheck
) throws Exception {
final Connection connection = i.connectionFactory.createConnection();
assertNotNull(connection);
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
assertNotNull(session);
final MessageProducer producer = session.createProducer(destination);
assertNotNull(producer);
final TextMessage message = session.createTextMessage();
assertNotNull(message);
message.setText("I am IronMan");
producer.send(message);
final List<ReceivedJmsMessage> messages = i.testQueue.drainReceivedMessages();
assertEquals(1, messages.size());
final ReceivedJmsMessage receivedMessage = messages.get(0);
destinationCheck.accept(receivedMessage.getDestination());
assertTrue(receivedMessage.getJmsMessage() instanceof TextMessage);
final TextMessage receivedTextMessage = (TextMessage) receivedMessage.getJmsMessage();
assertEquals("I am IronMan", receivedTextMessage.getText());
}
示例11: publiceerMaakSelectieResultaatTaken
import javax.jms.MessageProducer; //导入方法依赖的package包/类
@Override
public void publiceerMaakSelectieResultaatTaken(List<MaakSelectieResultaatTaak> maakSelectieResultaatTaken) {
LOGGER.info("publiceer maak selectie resultaat taken");
final ProducerCallback<Void> producerCallback = (final Session session, final MessageProducer producer) -> {
for (final MaakSelectieResultaatTaak maakSelectieResultaatTaak : maakSelectieResultaatTaken) {
LOGGER.debug("publiceer maak selectie resultaat taak");
final String
groupId =
maakSelectieResultaatTaak.getSelectieRunId() + "_" + maakSelectieResultaatTaak.getToegangLeveringsAutorisatieId() + "_"
+ maakSelectieResultaatTaak.getDienstId();
final Message message = session.createTextMessage(serializer.serialiseerNaarString(maakSelectieResultaatTaak));
message.setStringProperty(LeveringConstanten.JMS_MESSAGEGROUP_HEADER, groupId);
producer.send(message);
}
return null;
};
PublicatieHelper.publiceer(maakSelectieResultaatTemplate, producerCallback,
() -> "fout in verzenden berichten naar maak selectie resultaat taak queue");
}
示例12: testNullDestinationOnSendToAnonymousProducer
import javax.jms.MessageProducer; //导入方法依赖的package包/类
@Test
public void testNullDestinationOnSendToAnonymousProducer() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
MessageProducer producer = session.createProducer(null);
try {
producer.send(null, session.createMessage());
fail("Should not be able to send with null destination");
} catch (InvalidDestinationException ide) {}
}
示例13: sendReceiveForNode
import javax.jms.MessageProducer; //导入方法依赖的package包/类
private void sendReceiveForNode(String queueName, String hostname, String port) throws Exception {
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder("admin", "admin", hostname, port)
.withQueue(queueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
int numberOfMessages = 10;
for (int i = 0; i < numberOfMessages; i++) {
producer.send(producerSession.createTextMessage("Test message " + i));
}
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
for (int i = 0; i < numberOfMessages; i++) {
Message message = consumer.receive(5000);
Assert.assertNotNull(message, "Message #" + i + " was not received");
}
connection.close();
}
示例14: queueMessage
import javax.jms.MessageProducer; //导入方法依赖的package包/类
static void queueMessage(Session session, String text, String queueName) {
try {
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage(text);
message.setJMSCorrelationID(MDC.get(X_OBOS_REQUEST_ID));
producer.send(message);
session.commit();
} catch (JMSException ex) {
throw new MessageQueueException("Could not queue message '" + text + "'", ex);
}
}
示例15: main
import javax.jms.MessageProducer; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("HELLOWORLD.TESTQ");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}