public void onMessage(Message message) { try { MapMessage request = (MapMessage) message; String correlationID = request.getJMSCorrelationID(); int itemId = request.getInt("itemId"); // Retrieve the connection factory connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); // get the bids history String html = getBidHistory(itemId); // send the reply TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); if (temporaryTopic != null) { // create a connection connection = connectionFactory.createTopicConnection(); // create a session: no transaction, auto ack session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TextMessage reply = session.createTextMessage(); reply.setJMSCorrelationID(correlationID); reply.setText(html); replier = session.createPublisher(null); // unidentified publisher connection.start(); replier.publish(temporaryTopic, reply); replier.close(); session.close(); connection.stop(); connection.close(); } } catch (Exception e) { throw new EJBException("Message traitment failed for MDB_ViewBidHistory: " + e); } }
private TextMessage createMessage(String text) throws JMSException { TextMessage message = session.createTextMessage(); String streamId = (String) MDC.get(Constants.STREAM_ID); if (streamId != null) { message.setJMSCorrelationID(streamId); } message.setText(text); return message; }
public void txtSender(String content, String correlationid) throws JMSException { TextMessage txtMessage = session.createTextMessage(); txtMessage.setText(content); txtMessage.setJMSReplyTo(consumerQueue); String correlationId = correlationid; txtMessage.setJMSCorrelationID(correlationId); System.out.println(correlationId); this.producer.send(txtMessage); }
private void sendJMSMessageToKickOffRoute() throws Exception { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://testDlq"); factory.setWatchTopicAdvisories(false); Connection connection = factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(new ActiveMQQueue("fidEtpOrders")); TextMessage message = session.createTextMessage("Some Text, messageCount:" + messageCount++); message.setJMSCorrelationID("pleaseCorrelate"); producer.send(message); connection.close(); }
public void onMessage(Message message) { try { boolean accepted = false; // Get the data from the message MapMessage msg = (MapMessage) message; double salary = msg.getDouble("Salary"); double loanAmt = msg.getDouble("LoanAmount"); // Determine whether to accept or decline the loan if (loanAmt < 200000) { accepted = (salary / loanAmt) > .25; } else { accepted = (salary / loanAmt) > .33; } System.out.println( "" + "Percent = " + (salary / loanAmt) + ", loan is " + (accepted ? "Accepted!" : "Declined")); // Send the results back to the borrower TextMessage tmsg = qSession.createTextMessage(); tmsg.setText(accepted ? "Accepted!" : "Declined"); // correlation tmsg.setJMSCorrelationID(message.getJMSMessageID()); // Create the sender and send the message qSender = qSession.createSender((Queue) message.getJMSReplyTo()); qSender.send(tmsg); System.out.println("\nWaiting for loan requests..."); } catch (JMSException jmse) { jmse.printStackTrace(); System.exit(1); } catch (Exception jmse) { jmse.printStackTrace(); System.exit(1); } }
public void onMessage(Message message) { try { boolean accepted = false; // Get the data from the message MapMessage msg = (MapMessage) message; double salary = msg.getDouble("Salary"); double expAmt = msg.getDouble("Years’ experience"); // Determine whether to accept or decline the loan if (expAmt < 200000) { accepted = (salary / expAmt) > .25; } else { accepted = (salary / expAmt) > .33; } if (salary <= 32000) { accepted = true; } else { if (expAmt == 0) accepted = false; else accepted = ((double) (expAmt - 32000) / expAmt) < 3000.; } System.out.println(" Salary proposal is " + (accepted ? "Accepted!" : "Declined")); // Send the results back to the borrower TextMessage tmsg = qSession.createTextMessage(); tmsg.setText(accepted ? "Accepted!" : "Declined"); tmsg.setJMSCorrelationID(message.getJMSMessageID()); // Create the sender and send the message QueueSender qSender = qSession.createSender((Queue) message.getJMSReplyTo()); qSender.send(tmsg); System.out.println("\nWaiting for salary requests..."); } catch (JMSException jmse) { jmse.printStackTrace(); System.exit(1); } catch (Exception jmse) { jmse.printStackTrace(); System.exit(1); } }
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (testInfo()) { System.out.println("Log in successful, Welcome!"); Client client = new Client(userName.getText(), passWord.getText()); try { TextMessage textmessage = client.getSession().createTextMessage(); textmessage.setText(" "); textmessage.setJMSCorrelationID(userName.getText()); textmessage.setJMSReplyTo(client.getSession().createQueue(userName.getText())); System.out.println(userName.getText()); client.getProducer().send(Chatwindow.server.getServerTopic(), textmessage); } catch (JMSException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
public void onMessage(Message message) { try { TextMessage response = this._session.createTextMessage(); // Check we have the right message type. if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String messageText = txtMsg.getText(); // Perform the request System.out.println( "Received request:" + messageText + " for message :" + message.getJMSMessageID()); // Set the response back to the client response.setText("Response to Request:" + messageText); } // Set the correlation ID from the received message to be the correlation id of the response // message // this lets the client identify which message this is a response to if it has more than // one outstanding message to the server response.setJMSCorrelationID(message.getJMSMessageID()); try { System.out.println("Received message press enter to send response...."); new BufferedReader(new InputStreamReader(System.in)).readLine(); } catch (IOException e) { // Error attemptying to pause } // Send the response to the Destination specified by the JMSReplyTo field of the received // message. _replyProducer.send(message.getJMSReplyTo(), response); } catch (JMSException e) { // Handle the exception appropriately } _shutdownHook.countDown(); }
public void onMessage(Message message) { try { TextMessage response = this.session.createTextMessage(); if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String messageText = txtMsg.getText(); response.setText(this.messageProtocol.handleProtocolMessage(messageText)); } // Set the correlation ID from the received message to be the correlation id of the response // message // this lets the client identify which message this is a response to if it has more than // one outstanding message to the server response.setJMSCorrelationID(message.getJMSCorrelationID()); // Send the response to the Destination specified by the JMSReplyTo field of the received // message, // this is presumably a temporary queue created by the client this.replyProducer.send(message.getJMSReplyTo(), response); } catch (JMSException e) { // Handle the exception appropriately } }
protected void updateQueue(U bean) throws EventException { boolean resumeAfter = !awaitPaused; Session session = null; try { pause(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getSubmitQueueName()); QueueBrowser qb = session.createBrowser(queue); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { Message msg = (Message) e.nextElement(); TextMessage t = (TextMessage) msg; String json = t.getText(); final StatusBean b = service.unmarshal(json, getBeanClass()); MessageConsumer consumer = session.createConsumer(queue, "JMSMessageID = '" + msg.getJMSMessageID() + "'"); Message rem = consumer.receive(Constants.getReceiveFrequency()); consumer.close(); if (rem == null && b.getUniqueId().equals(bean.getUniqueId())) { // Something went wrong, not sure why it does this, TODO investigate if (overrideMap == null) overrideMap = new Hashtable<>(7); overrideMap.put(b.getUniqueId(), bean); continue; } MessageProducer producer = session.createProducer(queue); if (b.getUniqueId().equals(bean.getUniqueId())) { b.setStatus(bean.getStatus()); t = session.createTextMessage(service.marshal(b)); t.setJMSMessageID(rem.getJMSMessageID()); t.setJMSExpiration(rem.getJMSExpiration()); t.setJMSTimestamp(rem.getJMSTimestamp()); t.setJMSPriority(rem.getJMSPriority()); t.setJMSCorrelationID(rem.getJMSCorrelationID()); } producer.send(t); producer.close(); } } catch (Exception ne) { throw new EventException("Cannot reorder queue!", ne); } finally { // Only resume if it wasn't in a paused state before this update if (resumeAfter) { resume(); } try { if (session != null) session.close(); } catch (JMSException e) { throw new EventException("Cannot close session!", e); } } }
/** * This method establishes a connection to the ESB and also Instantiates a producer which can * deliver messages to the "ctms-caaers.inputQueue". */ public static void main(String[] args) { // Required fields ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; MessageProducer producer = null; StringBuilder contents = new StringBuilder(); BufferedReader input = null; File xmlFile = null; ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(); // Queue Instantiated with the Queue name from the ESB. ActiveMQQueue sendQueue = new ActiveMQQueue("ctms-caaers.inputQueue"); try { // Connection to the ESB. mqConnectionFactory.setBrokerURL("tcp://localhost:61616"); connectionFactory = mqConnectionFactory; connection = connectionFactory.createConnection(); // Obtaining a Session. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Instance of producer configured to deliver JMS Messages to the ""ctms-caaers.inputQueue" // Queue producer = session.createProducer(sendQueue); // For testing purpose the Study/Participant Xml is read from the file system. // In reality the Local Ctms System generates this Study/Participant Xml Message. xmlFile = getResources("classpath*:gov/nih/nci/cabig/caaers/impl/studydata/CreateStudyTest.xml")[0] .getFile(); // Reading the File Contents into Memory. input = new BufferedReader(new FileReader(xmlFile)); String line = null; while ((line = input.readLine()) != null) { contents.append(line); } // Create anew JMS TextMessage TextMessage message = session.createTextMessage(); // Here a sample ID is used. In reality the Local Ctms System has to have a // process to generate unique ID's. message.setJMSCorrelationID("103"); // Since this sample dealing with creating a Study in caaers the MESSAGE_TYPE is CREATE_STUDY. // If we need to update an existing Study in caaers then we have to set the MESSAGE_TYPE to // UPDATE_STUDY. message.setStringProperty("MESSAGE_TYPE", "CREATE_STUDY"); /* message.setStringProperty("MESSAGE_TYPE", "UPDATE_STUDY"); message.setStringProperty("MESSAGE_TYPE", "CREATE_PARTICIPANT"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_PARTICIPANT"); message.setStringProperty("MESSAGE_TYPE", "CREATE_INVESTIGATOR"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_INVESTIGATOR"); message.setStringProperty("MESSAGE_TYPE", "CREATE_RESEARCHSTAFF"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_RESEARCHSTAFF"); */ // Setting the File Contents in memory as the PayLoad of the JMS TextMessage. message.setText(contents.toString()); // Call to deliver the JMS Message to the Queue. producer.send(message); // Close producer.close(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { try { if (input != null) { input.close(); } if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } }