List of usage examples for org.springframework.jms.core SessionCallback SessionCallback
SessionCallback
From source file:samples.jms.sessioncallback.SessionCallbackExampleTests.java
@Test public void testSessionCallback() { jmsTemplate.execute(new SessionCallback<Object>() { public Object doInJms(Session session) throws JMSException { Queue queue = session.createQueue("someQueue"); MessageProducer producer = session.createProducer(queue); Message message = session.createTextMessage("Hello Queue!"); producer.send(message);/*from w ww .jav a 2 s.com*/ return null; } }); }
From source file:org.btc4j.jms.BtcDaemonCaller.java
public String sendReceive(final String destination, final String payload) { return jmsTemplate.execute(new SessionCallback<String>() { @Override//from ww w . j a va2s .c o m public String doInJms(Session session) throws JMSException { final TemporaryQueue replyQueue = session.createTemporaryQueue(); jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage message = session.createTextMessage(); message.setJMSReplyTo(replyQueue); message.setText(payload); message.setStringProperty("btcapi:account", account); message.setStringProperty("btcapi:password", password); return message; } }); return String.valueOf(jmsTemplate.receiveAndConvert(replyQueue)); } }); }
From source file:net.lr.jmsbridge.ConnectionPool.java
public Destination getReplyDestination(ConnectionFactory connectionFactory, UserNameAndPassword auth) { Destination replyDest = replyDestMap.get(auth); if (replyDest != null) { return replyDest; }// w ww .j a v a2s .c o m JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory(connectionFactory); replyDest = (Destination) jmsTemplate.execute(new SessionCallback() { @Override public Object doInJms(Session session) throws JMSException { return session.createTemporaryQueue(); } }); replyDestMap.put(auth, replyDest); return replyDest; }
From source file:org.calrissian.mango.jms.stream.AbstractJmsFileTransferSupport.java
@SuppressWarnings("unchecked") public void sendStream(Request req, final Destination replyTo) throws IOException { DigestInputStream is;/*from w w w . j av a 2s. c o m*/ Assert.notNull(req, "Request cannot be null"); final URI downloadUrl; try { downloadUrl = new URI(req.getDownloadUri()); } catch (URISyntaxException e) { throw new IOException(e); } try { is = new DigestInputStream(new BufferedInputStream(streamOpener.openStream(downloadUrl)), MessageDigest.getInstance(getHashAlgorithm())); } catch (NoSuchAlgorithmException e) { throw new JmsFileTransferException(e); } catch (Throwable e) { logger.info("Error occurred opening stream: " + e); return; } MessageQueueListener queueListener = null; try { @SuppressWarnings("rawtypes") Message returnMessage = (Message) jmsTemplate.execute(new SessionCallback() { @Override public Object doInJms(Session session) throws JMSException { DestinationRequestor requestor = null; try { Message responseMessage = DomainMessageUtils.toResponseMessage(session, new Response(ResponseStatusEnum.ACCEPT)); // Actual file transfer should be done on a queue. // Topics will not work Destination streamTransferDestination = factoryDestination(session, UUID.randomUUID().toString()); requestor = new DestinationRequestor(session, replyTo, streamTransferDestination, jmsTemplate.getReceiveTimeout()); Message returnMessage = requestor.request(responseMessage); requestor.close(); return returnMessage; } finally { if (requestor != null) requestor.close(); } } }, true); // timeout if (returnMessage == null) return; Response response = DomainMessageUtils.fromResponseMessage(returnMessage); // cancel transfer if (!ResponseStatusEnum.STARTSEND.equals(response.getStatus())) return; final Destination receiveAckDestination = returnMessage.getJMSDestination(); final Destination sendDataDestination = returnMessage.getJMSReplyTo(); queueListener = new MessageQueueListener(this, receiveAckDestination); logger.info("Sender[" + req.getRequestId() + "]: Starting send to: " + sendDataDestination); byte[] buffer = new byte[getPieceSize()]; int read = is.read(buffer); long placeInFile = 0; while (read >= 0) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); stream.write(buffer, 0, read); stream.close(); final byte[] pieceData = stream.toByteArray(); final Piece piece = new Piece(placeInFile, pieceData, getHashAlgorithm()); logger.info("Sender[" + req.getRequestId() + "]: Sending piece with position: " + piece.getPosition() + " Size of piece: " + pieceData.length); jmsTemplate.send(sendDataDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return DomainMessageUtils.toPieceMessage(session, piece); } }); // Message ret = jmsTemplate.receive(receiveAckDestination); Message ret = queueListener.getMessageInQueue(); logger.info("Sender[" + req.getRequestId() + "]: Sent piece and got ack"); // no one on the other end any longer, timeout if (ret == null) return; Response res = DomainMessageUtils.fromResponseMessage(ret); // stop transfer if (ResponseStatusEnum.RESEND.equals(res.getStatus())) { // resend piece logger.info("Sender[" + req.getRequestId() + "]: Resending piece"); } else if (ResponseStatusEnum.DENY.equals(res.getStatus())) { return; } else { buffer = new byte[getPieceSize()]; placeInFile += read; read = is.read(buffer); } } logger.info("Sender[" + req.getRequestId() + "]: Sending stop send"); final DigestInputStream fiIs = is; jmsTemplate.send(sendDataDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { Response stopSendResponse = new Response(ResponseStatusEnum.STOPSEND); stopSendResponse.setHash(new String(fiIs.getMessageDigest().digest())); return DomainMessageUtils.toResponseMessage(session, stopSendResponse); } }); Message ackMessage = queueListener.getMessageInQueue(); Object fromMessage = DomainMessageUtils.fromMessage(ackMessage); if (fromMessage instanceof Response) { Response ackResponse = (Response) fromMessage; if (ResponseStatusEnum.RESEND.equals(ackResponse.getStatus())) { // TODO: resend the whole file } } } catch (Exception e) { throw new JmsFileTransferException(e); } finally { try { is.close(); } catch (IOException ignored) { } if (queueListener != null) queueListener.close(); } }
From source file:org.calrissian.mango.jms.stream.AbstractJmsFileTransferSupport.java
@SuppressWarnings({ "unchecked", "rawtypes" }) protected Message sendWithResponse(final MessageCreator mc, final Destination replyTo) { return (Message) jmsTemplate.execute(new SessionCallback() { @Override/*from w w w. j a v a 2 s.co m*/ public Object doInJms(Session session) throws JMSException { DestinationRequestor requestor = null; try { Message sendMessage = mc.createMessage(session); requestor = new DestinationRequestor(session, replyTo, jmsTemplate.getReceiveTimeout()); Message returnMessage = requestor.request(sendMessage); requestor.close(); return returnMessage; } finally { if (requestor != null) requestor.close(); } } }, true); }
From source file:org.apache.servicemix.jms.endpoints.AbstractConsumerEndpoint.java
protected void processExchange(final MessageExchange exchange, final Session session, final JmsContext context) throws Exception { // Ignore DONE exchanges if (exchange.getStatus() == ExchangeStatus.DONE) { return;/*w w w .j a v a 2 s . c om*/ } // Create session if needed if (session == null) { template.execute(new SessionCallback() { public Object doInJms(Session session) throws JMSException { try { processExchange(exchange, session, context); } catch (Exception e) { throw new ListenerExecutionFailedException("Exchange processing failed", e); } return null; } }); return; } // Handle exchanges Message msg = null; Destination dest = null; if (exchange.getStatus() == ExchangeStatus.ACTIVE) { if (exchange.getFault() != null) { msg = marshaler.createFault(exchange, exchange.getFault(), session, context); dest = getReplyDestination(exchange, exchange.getFault(), session, context); } else if (exchange.getMessage("out") != null) { msg = marshaler.createOut(exchange, exchange.getMessage("out"), session, context); dest = getReplyDestination(exchange, exchange.getMessage("out"), session, context); } if (msg == null) { throw new IllegalStateException("Unable to send back answer or fault"); } setCorrelationId(context.getMessage(), msg); try { send(msg, session, dest); done(exchange); } catch (Exception e) { fail(exchange, e); throw e; } } else if (exchange.getStatus() == ExchangeStatus.ERROR) { Exception error = exchange.getError(); if (error == null) { error = new JBIException("Exchange in ERROR state, but no exception provided"); } msg = marshaler.createError(exchange, error, session, context); dest = getReplyDestination(exchange, error, session, context); setCorrelationId(context.getMessage(), msg); send(msg, session, dest); } else { throw new IllegalStateException("Unrecognized exchange status"); } }
From source file:org.apache.servicemix.jms.endpoints.JmsProviderEndpoint.java
/** * Process an InOnly or RobustInOnly exchange. * This method uses the JMS template to create a session and call the * {@link #processInOnlyInSession(javax.jbi.messaging.MessageExchange, javax.jbi.messaging.NormalizedMessage, javax.jms.Session)} * method./*from w w w . j a v a2 s . c om*/ * * @param exchange * @param in * @throws Exception */ protected void processInOnly(final MessageExchange exchange, final NormalizedMessage in) throws Exception { SessionCallback callback = new SessionCallback() { public Object doInJms(Session session) throws JMSException { try { processInOnlyInSession(exchange, in, session); return null; } catch (JMSException e) { throw e; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new UncategorizedJmsException(e); } } }; template.execute(callback, true); }
From source file:com.ccc.ccm.client.JMSTemplateAutowired.java
public <T> T execute(final Destination destination, final ProducerCallback<T> action) throws JmsException { Assert.notNull(action, "Callback object must not be null"); return execute(new SessionCallback<T>() { public T doInJms(Session session) throws JMSException { MessageProducer producer = createProducer(session, destination); try { return action.doInJms(session, producer); } finally { JmsUtils.closeMessageProducer(producer); }/* ww w .ja va2 s .c o m*/ } }, false); }
From source file:com.ccc.ccm.client.JMSTemplateAutowired.java
public <T> T execute(final String destinationName, final ProducerCallback<T> action) throws JmsException { Assert.notNull(action, "Callback object must not be null"); return execute(new SessionCallback<T>() { public T doInJms(Session session) throws JMSException { Destination destination = resolveDestinationName(session, destinationName); MessageProducer producer = createProducer(session, destination); try { return action.doInJms(session, producer); } finally { JmsUtils.closeMessageProducer(producer); }//from ww w . ja v a2 s . c o m } }, false); }
From source file:com.ccc.ccm.client.JMSTemplateAutowired.java
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException { execute(new SessionCallback<Object>() { public Object doInJms(Session session) throws JMSException { doSend(session, destination, messageCreator); return null; }//ww w. j av a 2 s .c o m }, false); }