本文整理汇总了Java中javax.jms.MessageProducer类的典型用法代码示例。如果您正苦于以下问题:Java MessageProducer类的具体用法?Java MessageProducer怎么用?Java MessageProducer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MessageProducer类属于javax.jms包,在下文中一共展示了MessageProducer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putTopic
import javax.jms.MessageProducer; //导入依赖的package包/类
private void putTopic(List<String> events) throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME,
PASSWORD, BROKER_BIND_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic(DESTINATION_NAME);
MessageProducer producer = session.createProducer(destination);
for (String event : events) {
TextMessage message = session.createTextMessage();
message.setText(event);
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
示例2: receiveAndRespondWithMessageIdAsCorrelationId
import javax.jms.MessageProducer; //导入依赖的package包/类
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
final javax.jms.Message inMessage = consumer.receive();
String requestMessageId = inMessage.getJMSMessageID();
LOG.debug("Received message " + requestMessageId);
final TextMessage replyMessage = session.createTextMessage("Result");
replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
LOG.debug("Sending reply to " + inMessage.getJMSReplyTo());
producer.send(replyMessage);
producer.close();
consumer.close();
session.close();
connection.close();
}
示例3: testFailedCreateConsumerConnectionStillWorks
import javax.jms.MessageProducer; //导入依赖的package包/类
@Test
public void testFailedCreateConsumerConnectionStillWorks() throws JMSException {
Connection connection = pooledConnFact.createConnection("guest", "password");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(name.getMethodName());
try {
session.createConsumer(queue);
fail("Should fail to create consumer");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
queue = session.createQueue("GUESTS." + name.getMethodName());
MessageProducer producer = session.createProducer(queue);
producer.close();
connection.close();
}
示例4: JmsPoolMessageProducer
import javax.jms.MessageProducer; //导入依赖的package包/类
public JmsPoolMessageProducer(JmsPoolSession session, MessageProducer messageProducer, Destination destination, boolean shared) throws JMSException {
this.session = session;
this.messageProducer = messageProducer;
this.destination = destination;
this.shared = shared;
this.anonymousProducer = destination == null;
this.deliveryMode = messageProducer.getDeliveryMode();
this.disableMessageID = messageProducer.getDisableMessageID();
this.disableMessageTimestamp = messageProducer.getDisableMessageTimestamp();
this.priority = messageProducer.getPriority();
this.timeToLive = messageProducer.getTimeToLive();
if (session.isJMSVersionSupported(2, 0)) {
this.deliveryDelay = messageProducer.getDeliveryDelay();
}
}
示例5: 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());
}
}
示例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: 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());
}
示例8: 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);
}
}
示例9: 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);
}
}
示例10: createMessageProducer
import javax.jms.MessageProducer; //导入依赖的package包/类
public static MessageProducer createMessageProducer(
Session session,
Destination destination,
MessageProducerOption producerOption) throws JMSException {
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryDelay(producerOption.getDeliveryDelay());
producer.setDeliveryMode(producerOption.getDeliveryMode());
producer.setDisableMessageTimestamp(producerOption.isDisableMessageTimestamp());
producer.setDisableMessageID(producerOption.isDisableMessageId());
producer.setPriority(producerOption.getPriority());
producer.setTimeToLive(producerOption.getTimeToLive());
return producer;
}
示例11: create
import javax.jms.MessageProducer; //导入依赖的package包/类
/**
* @param session
* @param messageProducer
* @param dupMessageDetectStrategy
* @return
* @throws JMSException
*/
public static SimpleJmsMessageSender create(
Session session,
MessageProducer messageProducer,
DupMessageDetectStrategy dupMessageDetectStrategy
) throws JMSException {
SimpleJmsMessageSender messageSender = new SimpleJmsMessageSender();
messageSender.setMessageProducer(messageProducer);
messageSender.setSession(session);
if (dupMessageDetectStrategy != null) {
messageSender.setDupMessageDetectStrategy(dupMessageDetectStrategy);
}
return messageSender;
}
示例12: 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);
}
}
示例13: main
import javax.jms.MessageProducer; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Connection connection = null;
String csvData = System.getProperty(CSVDATA);
if(CSVDATA == null || CSVDATA.equals(""))
throw new RuntimeException("LoyaltyCardManager.main() must pass the "+CSVDATA +" system property With format OPERATION;USERID;FIRSTNAME;LASTNAME;TRXID;TRXFEESAMOUNT;CURRENCY");
System.out.println("LoyaltyCardManager() will connect to router: "+ROUTER_URL+" : at the following address: "+QUEUE_NAME);
ConnectionFactory connectionFactory = new JmsConnectionFactory(ROUTER_URL);
try {
// Step 1. Create an AMQP qpid connection
connection = connectionFactory.createConnection();
// Step 2. Create a JMS session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 3. Create a Producer
Queue fidelityRequestQueue = session.createQueue(QUEUE_NAME);
MessageProducer beosbankFidelityRequestProducer = session.createProducer(fidelityRequestQueue);
// Step 4. send a CSV Text Data on user transactions
beosbankFidelityRequestProducer.send(session.createTextMessage(csvData));
System.out.println("\nmessage sent:"+ csvData+" \n");
} finally {
if (connection != null) {
// Step 9. close the connection
connection.close();
}
}
}
示例14: 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);
}
}
示例15: send
import javax.jms.MessageProducer; //导入依赖的package包/类
/** Writes a JMS Message to the destination, as defined in the configuration file.
* @param session the JMS Session.
* @param message the JMS Message.
* @param destinationName the Destination to write to.
* @throws FrameworkException Indicates some system error.
* @throws ApplicationExceptions Indicates application error(s).
*/
static void send(Session session, Message message, String destinationName) throws FrameworkException, ApplicationExceptions {
try {
Destination destination = obtainDestination(destinationName);
MessageProducer producer = session.createProducer(destination);
int messagePriority = message.getJMSPriority();
if (messagePriority != producer.getPriority() && messagePriority >= 0 && messagePriority <= 9)
producer.setPriority(messagePriority);
producer.send(message);
if (log.isDebugEnabled())
log.debug("Sent message " + message + " to " + destinationName);
} catch (JMSException e) {
log.error("Error in sending a JMS Message", e);
throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.SEND_ERROR, null, e);
}
}