public static void main(String[] args) { try { // Create and start connection InitialContext ctx = new InitialContext(); QueueConnectionFactory f = (QueueConnectionFactory) ctx.lookup("myQueueConnectionFactory"); QueueConnection con = f.createQueueConnection(); con.start(); // 2) create queue session QueueSession ses = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); // 3) get the Queue object Queue t = (Queue) ctx.lookup("myQueue"); // 4)create QueueSender object QueueSender sender = ses.createSender(t); // 5) create TextMessage object TextMessage msg = ses.createTextMessage(); // 6) write message BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Enter Msg, end to terminate:"); String s = b.readLine(); if (s.equals("end")) break; msg.setText(s); // 7) send message sender.send(msg); System.out.println("Message successfully sent."); } // 8) connection close con.close(); } catch (Exception e) { System.out.println(e); } }
public void onMessage(Message inMessage) { TextMessage msg = null; try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println("MESSAGE BEAN: Message received: " + msg.getText()); long sleepTime = msg.getLongProperty("sleeptime"); System.out.println("Sleeping for : " + sleepTime + " milli seconds "); Thread.sleep(sleepTime); queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueSender = queueSession.createSender(queue); TextMessage message = queueSession.createTextMessage(); message.setText("REPLIED:" + msg.getText()); message.setIntProperty("replyid", msg.getIntProperty("id")); System.out.println("Sending message: " + message.getText()); queueSender.send(message); } else { System.out.println("Message of wrong type: " + inMessage.getClass().getName()); } } catch (JMSException e) { e.printStackTrace(); } catch (Throwable te) { te.printStackTrace(); } finally { try { queueSession.close(); queueConnection.close(); } catch (Exception e) { } } } // onMessage
public void sendReply(Message request, int sum) { try { MessageProducer MP = session.createProducer(null); Destination reply = request.getJMSReplyTo(); TextMessage TM = session.createTextMessage(); TM.setText("" + sum); MP.send(reply, TM); } catch (JMSException JMSE) { System.out.println("JMS Exception: " + JMSE); } }
public void onMessage(Message message) { TextMessage TM = (TextMessage) message; try { if (TM.getText().equalsIgnoreCase("issuemovie")) { int newsum = counter.issuemovie(); sendReply(message, newsum); } else if (TM.getText().equalsIgnoreCase("getsum_issued_movie")) { int sum = counter.getsum_issued_movies(); sendReply(message, sum); } } catch (JMSException JMSE) { System.out.println("JMS Exception: " + JMSE); } }
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); } }
public static void main(String[] args) { try { QueueConnectionFactory queueConnectionFactory; QueueConnection queueConnection; QueueSession queueSession; QueueReceiver queueReceiver; Queue queue; TextMessage msg; // JNDI InitialContextを作成します InitialContext ctx = new InitialContext(); // Connection FactoryとQueueをLook upします queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); queue = (Queue) ctx.lookup(QUEUE); // コネクションを作成 queueConnection = queueConnectionFactory.createQueueConnection(); // セッションを作成 queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); // キューレシーバーを作成 queueReceiver = queueSession.createReceiver(queue); // メッセージの配送をスタート queueConnection.start(); // メッセージの受信 while (true) { Message m = queueReceiver.receive(1); if (m != null) { if (m instanceof TextMessage) { msg = (TextMessage) m; System.out.println(msg.getText()); } else { break; } } } // 接続を切断 queueReceiver.close(); queueSession.close(); queueConnection.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * Send a message to controlQueue. Called by a subscriber to notify a publisher that it is ready * to receive messages. * * <p>If controlQueue doesn't exist, the method throws an exception. * * @param prefix prefix (publisher or subscriber) to be displayed * @param controlQueueName name of control queue */ public static void sendSynchronizeMessage(String prefix, String controlQueueName) throws Exception { QueueConnectionFactory queueConnectionFactory = null; QueueConnection queueConnection = null; QueueSession queueSession = null; Queue controlQueue = null; QueueSender queueSender = null; TextMessage message = null; try { queueConnectionFactory = SampleUtilities.getQueueConnectionFactory(); queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); controlQueue = getQueue(controlQueueName, queueSession); } catch (Exception e) { System.out.println("Connection problem: " + e.toString()); if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException ee) { } } throw e; } try { queueSender = queueSession.createSender(controlQueue); message = queueSession.createTextMessage(); message.setText("synchronize"); System.out.println(prefix + "Sending synchronize message to " + controlQueueName); queueSender.send(message); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); throw e; } finally { if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException e) { } } } }