本文整理匯總了Java中javax.jms.QueueSession.createTextMessage方法的典型用法代碼示例。如果您正苦於以下問題:Java QueueSession.createTextMessage方法的具體用法?Java QueueSession.createTextMessage怎麽用?Java QueueSession.createTextMessage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.QueueSession
的用法示例。
在下文中一共展示了QueueSession.createTextMessage方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: invokeJMS
import javax.jms.QueueSession; //導入方法依賴的package包/類
/** Send a JSON message to our notification queue.
*/
public void invokeJMS(JsonObject json) throws JMSException, NamingException {
if (!initialized) initialize(); //gets our JMS managed resources (Q and QCF)
QueueConnection connection = queueCF.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage message = session.createTextMessage(json.toString());
System.out.println("Sending "+json.toString()+" to "+queue.getQueueName());
QueueSender sender = session.createSender(queue);
sender.send(message);
sender.close();
session.close();
connection.close();
System.out.println("Message sent successfully!");
}
示例2: acknowledge
import javax.jms.QueueSession; //導入方法依賴的package包/類
private void acknowledge(Message jmsMessage, String msgid) throws JMSException, ServiceLocatorException {
QueueConnection connection = null;
QueueSession session = null;
QueueSender sender = null;
try {
Queue respQueue = (Queue) jmsMessage.getJMSReplyTo();
QueueConnectionFactory qcf = JMSServices.getInstance().getQueueConnectionFactory(null);
connection = qcf.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.DUPS_OK_ACKNOWLEDGE);
sender = session.createSender(respQueue);
Message respMsg = session.createTextMessage(msgid);
// respMsg.setJMSCorrelationID(correlationId); not used
sender.send(respMsg);
} finally {
if (sender != null) sender.close();
if (session != null) session.close();
if (connection != null) connection.close();
}
}
示例3: sendMessage
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* Send a message to testInboundQueue queue
*
* @throws Exception
*/
private void sendMessage() throws Exception {
InitialContext initialContext = JmsClientHelper.getActiveMqInitialContext();
QueueConnectionFactory connectionFactory
= (QueueConnectionFactory) initialContext.lookup(JmsClientHelper.QUEUE_CONNECTION_FACTORY);
QueueConnection queueConnection = connectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(queueSession.createQueue(QUEUE_NAME));
String message = "<?xml version='1.0' encoding='UTF-8'?>" +
" <ser:getQuote xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\"> " +
" <ser:request>" +
" <xsd:symbol>IBM</xsd:symbol>" +
" </ser:request>" +
" </ser:getQuote>";
try {
TextMessage jmsMessage = queueSession.createTextMessage(message);
jmsMessage.setJMSType("incorrecttype");
sender.send(jmsMessage);
} finally {
queueConnection.close();
}
}
示例4: publishMessagesToQueue
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* To publish the messages to a queue.
*
* @throws JMSException JMS Exception.
* @throws InterruptedException Interrupted exception while waiting in between messages.
*/
public void publishMessagesToQueue(String queueName) throws JMSException, InterruptedException {
QueueConnection queueConn = (QueueConnection) connectionFactory.createConnection();
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = queueSession.createQueue(queueName);
MessageProducer queueSender = queueSession.createProducer(destination);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int index = 0; index < 10; index++) {
String queueText = "Queue Message : " + (index + 1);
TextMessage queueMessage = queueSession.createTextMessage(queueText);
queueSender.send(queueMessage);
Thread.sleep(1000);
logger.info("Publishing " + queueText + " to queue " + queueName);
}
queueConn.close();
queueSession.close();
queueSender.close();
}
示例5: testCreateReceiverWithMessageSelector
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* com.sun.ts.tests.jms.ee.all.queueconn.QueueConnTest line 171
*/
@Test
public void testCreateReceiverWithMessageSelector() throws Exception {
QueueConnection qc = null;
try {
qc = createQueueConnection();
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueReceiver qreceiver = qs.createReceiver(queue1, "targetMessage = TRUE");
qc.start();
TextMessage m = qs.createTextMessage();
m.setText("one");
m.setBooleanProperty("targetMessage", false);
QueueSender qsender = qs.createSender(queue1);
qsender.send(m);
m.setText("two");
m.setBooleanProperty("targetMessage", true);
qsender.send(m);
TextMessage rm = (TextMessage) qreceiver.receive(1000);
ProxyAssertSupport.assertEquals("two", rm.getText());
} finally {
if (qc != null) {
qc.close();
}
Thread.sleep(2000);
removeAllMessages(queue1.getQueueName(), true);
checkEmpty(queue1);
}
}
示例6: sendReply
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* Intended for server-side reponse sending and implies that the received
* message *always* contains a reply-to address.
*/
public void sendReply(QueueSession session, Message received_message, String response) throws IfsaException {
QueueSender tqs=null;
try {
TextMessage answer = session.createTextMessage();
answer.setText(response);
Queue replyQueue = (Queue)received_message.getJMSReplyTo();
tqs = session.createSender(replyQueue );
if (log.isDebugEnabled()) log.debug(getLogPrefix()+ "sending reply to ["+ received_message.getJMSReplyTo()+ "]");
((IFSAServerQueueSender) tqs).sendReply(received_message, answer);
} catch (Throwable t) {
throw new IfsaException(t);
} finally {
if (tqs!=null) {
try {
tqs.close();
} catch (JMSException e) {
log.warn(getLogPrefix()+ "exception closing reply queue sender",e);
}
}
}
}
示例7: avisoBackOffice
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* Realiza proceso de aviso a BackOffice para un tr�mite. Metemos en cola as�ncrona.
* En caso de error lanza mensaje al log y permite continuar.
* @param procedimiento
*/
private void avisoBackOffice(Procedimiento procedimiento,Date hasta) throws Exception{
log.debug("Aviso a backoffice procedimiento " + procedimiento.getIdentificador() + " (hasta " + StringUtil.fechaACadena(hasta,StringUtil.FORMATO_TIMESTAMP) + ")");
// Obtenemos entradas no procesadas
TramiteBandejaDelegate tbd = (TramiteBandejaDelegate) DelegateUtil.getTramiteBandejaDelegate();
// Obtenemos tramites del procedimiento que tienen entradas pendientes y generamos un mensaje por tramite
String idTramites[] = tbd.obtenerIdTramitesProcedimiento(procedimiento.getIdentificador(),ConstantesBTE.ENTRADA_NO_PROCESADA,null,hasta);
if (idTramites != null) {
for (int i = 0; i < idTramites.length; i++) {
String entradas [] = tbd.obtenerNumerosEntradas(procedimiento.getIdentificador(), idTramites[i], ConstantesBTE.ENTRADA_NO_PROCESADA,null,hasta);
log.debug("Aviso de " + entradas.length + " nuevas entradas para backoffice tr�mite " + idTramites[i] + " hasta " + StringUtil.fechaACadena(hasta,StringUtil.FORMATO_TIMESTAMP) + " (Procedimiento: " + procedimiento.getIdentificador() + ")");
if (entradas.length > 0){
// Dejamos entrada en la cola de avisos
InitialContext ctx = new InitialContext();
String colaAvisos = (String) ctx.lookup("java:comp/env/colaAvisos");
Queue queue = (Queue) ctx.lookup(colaAvisos);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/XAConnectionFactory");
QueueConnection cnn = factory.createQueueConnection();
QueueSession sess = cnn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = sess.createTextMessage(BteStringUtil.numeroEntradasToString(entradas));
QueueSender sender = sess.createSender(queue);
sender.send(msg,DeliveryMode.NON_PERSISTENT,4,0);
}
}
}
}
示例8: avisoBackOffice
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* Tras producirse una entrada en el BackOffice realizamos aviso inmediato si procede
* @param entrada
*/
private void avisoBackOffice(String numeroEntrada) throws Exception{
// TODO Destino cola parametrizable por properties
// Recuperamos informacion tramite
TramiteBandejaDelegate td = DelegateUtil.getTramiteBandejaDelegate();
TramiteBandeja tramiteBandeja = td.obtenerTramiteBandeja(numeroEntrada);
// Comprobamos si tiene habilitado el aviso inmediato
if (tramiteBandeja.getProcedimiento().avisosEnabled() &&
tramiteBandeja.getProcedimiento().getInmediata() == 'S'){
// Dejamos entrada en la cola de avisos inmediatos
InitialContext ctx = new InitialContext();
String colaAvisos = (String) ctx.lookup("java:comp/env/colaAvisos");
Queue queue = (Queue) ctx.lookup(colaAvisos);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("java:/JmsXA");
QueueConnection cnn = factory.createQueueConnection();
// Creamos sesion transacted para que hasta q no se haga commit no se env�e el mensaje
QueueSession sess = cnn.createQueueSession(true,0);
TextMessage msg = sess.createTextMessage(numeroEntrada);
QueueSender sender = sess.createSender(queue);
sender.send(msg);
cnn.close();
}
}
示例9: ignoreOnMessage
import javax.jms.QueueSession; //導入方法依賴的package包/類
public void ignoreOnMessage()
throws Exception
{
Email email = new Email();
email.sendMail(to, from, subject, "PRE TEST");
Debugger.println(this,"Hello word");
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;
InitialContext ctx = new InitialContext();
Queue queue = (Queue) ctx.lookup("queue/tutorial/example");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
try {
cnn = factory.createQueueConnection();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = session.createTextMessage("Hello World");
msg.setStringProperty("to", to);
msg.setStringProperty("from", from);
msg.setStringProperty("subject", subject);
sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");
} finally {
// Cleanup
// close the connection
if (cnn != null)
{
cnn.close();
}
}
}
示例10: storno
import javax.jms.QueueSession; //導入方法依賴的package包/類
public String storno() {
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession sess = null;
Queue queue = null;
try {
InitialContext ctx = new InitialContext(System.getProperties());
// Der Name der JMS-Queue wurde in eine <managed-property>
// (in faces-config.xml) ausgelagert, um ihn beim Deployment
// an eine konkrete Queue in einer Applikationsserver-Instanz
// anpassen zu k�nnen.
// F�r die Message-Driven Bean ist der Queue-Name im
// Deployment-Deskriptor ejb-jar.xml festgelegt.
queue = (Queue) ctx.lookup(this.messageQueue);
QueueConnectionFactory factory = (QueueConnectionFactory) ctx
.lookup("ConnectionFactory");
cnn = factory.createQueueConnection();
sess = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = sess.createTextMessage("STORNO "
+ this.bestellnummer);
// The sent timestamp acts as the message's ID
long sent = System.currentTimeMillis();
msg.setLongProperty("sent", sent);
logger.debug("Sending message: " + msg);
sender = sess.createSender(queue);
sender.send(msg);
sess.close();
} catch (Exception e) {
logger.error(e);
}
return "stornierungsergebnis";
}
示例11: run
import javax.jms.QueueSession; //導入方法依賴的package包/類
public void run() {
QueueConnection connection = null;
StandardLogger logger = LoggerUtil.getStandardLogger();
try {
String txt = message.getText();
if (logger.isDebugEnabled()) {
logger.debug("JMS Listener receives request: " + txt);
}
String resp;
ListenerHelper helper = new ListenerHelper();
Map<String, String> metaInfo = new HashMap<String, String>();
metaInfo.put(Listener.METAINFO_PROTOCOL, Listener.METAINFO_PROTOCOL_JMS);
metaInfo.put(Listener.METAINFO_REQUEST_PATH, getQueueName());
metaInfo.put(Listener.METAINFO_SERVICE_CLASS, this.getClass().getName());
metaInfo.put(Listener.METAINFO_REQUEST_ID, message.getJMSMessageID());
metaInfo.put(Listener.METAINFO_CORRELATION_ID, message.getJMSCorrelationID());
if (message.getJMSReplyTo() != null)
metaInfo.put("ReplyTo", message.getJMSReplyTo().toString());
resp = helper.processEvent(txt, metaInfo);
Queue respQueue = (Queue) message.getJMSReplyTo();
String correlId = message.getJMSCorrelationID();
if (resp != null && respQueue != null && !isCertifiedMessage(correlId)) {
// String msgId = jmsMessage.getJMSMessageID();
QueueConnectionFactory qcf
= JMSServices.getInstance().getQueueConnectionFactory(null);
connection = qcf.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(respQueue);
Message respMsg = session.createTextMessage(resp);
respMsg.setJMSCorrelationID(correlId);
sender.send(respMsg);
if (logger.isDebugEnabled()) {
logger.debug("JMS Listener sends response (corr id='" +
correlId + "'): " + resp);
}
}
}
catch (Throwable ex) {
logger.severeException(ex.getMessage(), ex);
}
finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
logger.severeException(e.getMessage(), e);
}
}
}
}
示例12: sendTextMessage
import javax.jms.QueueSession; //導入方法依賴的package包/類
/**
* Sends a JMS text message.
*
* @param contextUrl
* null for local queues
* @param queueName
* local queues are based on logical queue names
* @param message
* the message string
* @param delaySeconds
* 0 for immediate
* @throws ServiceLocatorException
*/
public void sendTextMessage(String contextUrl, String queueName, String message,
int delaySeconds, String correlationId)
throws NamingException, JMSException, ServiceLocatorException {
if (logger.isDebugEnabled())
logger.debug("Send JMS message: " + message);
if (mdwMessageProducer != null) {
if (logger.isDebugEnabled())
logger.debug("Send JMS message: queue " + queueName + " corrId " + correlationId
+ " delay " + delaySeconds);
mdwMessageProducer.sendMessage(message, queueName, correlationId, delaySeconds,
DeliveryMode.NON_PERSISTENT);
}
else {
QueueConnection connection = null;
QueueSession session = null;
QueueSender sender = null;
Queue queue = null;
try {
QueueConnectionFactory connectionFactory = getQueueConnectionFactory(contextUrl);
connection = connectionFactory.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
if (contextUrl == null)
queue = getQueue(session, queueName);
else
queue = getQueue(contextUrl, queueName);
if (queue == null)
queue = session.createQueue(queueName);
sender = session.createSender(queue);
TextMessage textMessage = session.createTextMessage(message);
if (delaySeconds > 0)
jmsProvider.setMessageDelay(sender, textMessage, delaySeconds);
if (correlationId != null)
textMessage.setJMSCorrelationID(correlationId);
connection.start();
if (contextUrl == null)
sender.send(textMessage, DeliveryMode.NON_PERSISTENT, sender.getPriority(),
sender.getTimeToLive());
else
sender.send(textMessage);
// }
// catch(ServiceLocatorException ex) {
// logger.severeException(ex.getMessage(), ex);
// JMSException jmsEx = new JMSException(ex.getMessage());
// jmsEx.setLinkedException(ex);
// throw jmsEx;
}
finally {
closeResources(connection, session, sender);
}
}
}
示例13: invoke
import javax.jms.QueueSession; //導入方法依賴的package包/類
public String invoke(String contextUrl, String reqqueue_name, String request,
int timeoutSeconds, String correlationId)
throws NamingException, JMSException, ServiceLocatorException {
QueueConnection connection = null;
QueueSession session = null;
QueueSender sender = null;
Queue reqqueue = null;
Queue respqueue = null;
try {
if (logger.isDebugEnabled())
logger.debug("Invoke jms request message: " + request);
QueueConnectionFactory connectionFactory = getQueueConnectionFactory(contextUrl);
connection = connectionFactory.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
if (contextUrl == null) {
reqqueue = getQueue(session, reqqueue_name);
}
else {
reqqueue = getQueue(contextUrl, reqqueue_name);
if (reqqueue == null) {
// jndi lookup does not work for ActiveMQ
reqqueue = getQueue(session, reqqueue_name);
}
}
respqueue = session.createTemporaryQueue();
sender = session.createSender(reqqueue);
TextMessage textMessage = session.createTextMessage();
if (correlationId != null)
textMessage.setJMSCorrelationID(correlationId);
textMessage.setJMSReplyTo(respqueue);
connection.start();
textMessage.setText(request);
sender.send(textMessage);
MessageConsumer consumer = session.createConsumer(respqueue);
textMessage = (TextMessage) consumer.receive(timeoutSeconds * 1000);
if (textMessage == null) {
throw new JMSException("Synchronous JMS call times out while waiting for response");
}
else {
return textMessage.getText();
}
}
// catch(ServiceLocatorException ex) {
// ex.printStackTrace(); // cannot use logger for chicken-egg issue
// JMSException jmsEx = new JMSException(ex.getMessage());
// jmsEx.setLinkedException(ex);
// throw jmsEx;
// }
finally {
closeResources(connection, session, sender);
}
}
示例14: main
import javax.jms.QueueSession; //導入方法依賴的package包/類
public static void main(String[] args) {
String queueConnectionFactoryName = "myjmsconnectionfactory"; //JMS Connection Factory的JNDI
String queueName = "myjmsqueue"; //JMS Queue或者JMS Topic的JNDI
boolean transacted = false;//transaction模式
int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式
String message="Message need to send";//模擬需要發送的消息
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
Context context = new InitialContext(properties);
Object obj = context.lookup(queueConnectionFactoryName);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的獲得
obj = context.lookup(queueName);
Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的獲得
QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//產生連接
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
TextMessage textMessage = queueSession.createTextMessage();
textMessage.clearBody();
textMessage.setText(message);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.send(textMessage);
if (transacted) {
queueSession.commit();
}
if (queueSender != null) {
queueSender.close();
}
if (queueSession != null) {
queueSession.close();
}
if (queueConnection != null) {
queueConnection.close();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
示例15: main
import javax.jms.QueueSession; //導入方法依賴的package包/類
public static void main(final String[] args) throws Exception {
QueueConnection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the Connection Factory
QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4.Create a JMS Connection
connection = cf.createQueueConnection();
// Step 5. Create a JMS Session
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
// Step 7. Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
System.out.println("Sent message: " + message.getText());
// Step 8. Send the Message
producer.send(message);
// Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage
ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
// Step 10. Create JMX Connector to connect to the server's MBeanServer
//we dont actually need credentials as the guest login i sused but this is how its done
HashMap env = new HashMap();
String[] creds = {"guest", "guest"};
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMXExample.JMX_URL), env);
// Step 11. Retrieve the MBeanServerConnection
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
// Step 12. Create a QueueControl proxy to manage the queue on the server
QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
// Step 13. Display the number of messages in the queue
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
// Step 14. Remove the message sent at step #8
System.out.println("message has been removed: " + queueControl.removeMessage(((ActiveMQTextMessage) message).getCoreMessage().getMessageID()));
// Step 15. Display the number of messages in the queue
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
// Step 16. We close the JMX connector
connector.close();
// Step 17. Create a JMS Message Consumer on the queue
MessageConsumer messageConsumer = session.createConsumer(queue);
// Step 18. Start the Connection
connection.start();
// Step 19. Trying to receive a message. Since the only message in the queue was removed by a management
// operation, there is none to consume.
// The call will timeout after 5000ms and messageReceived will be null
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived);
} finally {
// Step 20. Be sure to close the resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}