public void testSubscribeWithMessageSentWithProperties() throws Exception { String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame); frame = receiveFrame(100000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SUBSCRIBE\n" + "destination:/queue/" + getQueueName() + "\n" + "ack:auto\n\n" + Stomp.NULL; sendFrame(frame); MessageProducer producer = session.createProducer(queue); TextMessage message = session.createTextMessage("Hello World"); message.setStringProperty("s", "value"); message.setBooleanProperty("n", false); message.setByteProperty("byte", (byte) 9); message.setDoubleProperty("d", 2.0); message.setFloatProperty("f", (float) 6.0); message.setIntProperty("i", 10); message.setLongProperty("l", 121); message.setShortProperty("s", (short) 12); producer.send(message); frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("MESSAGE")); // System.out.println("out: "+frame); frame = "DISCONNECT\n" + "\n\n" + Stomp.NULL; sendFrame(frame); }
private void scheduleOneShot(Connection connection) throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("test.queue"); MessageProducer producer = session.createProducer(queue); TextMessage message = session.createTextMessage("test msg"); long time = TimeUnit.SECONDS.toMillis(30); message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time); message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 0); producer.send(message); producer.close(); }
private void scheduleRepeating(Connection connection) throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("test.queue"); MessageProducer producer = session.createProducer(queue); TextMessage message = session.createTextMessage("test msg"); long time = 360 * 1000; message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time); message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 500); message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, -1); producer.send(message); producer.close(); }
private TextMessage createEventMessage(LoggingEvent event, String service, String environment) throws JMSException { TextMessage message = createMessage(this.layout.format(event)); message.setStringProperty(Constants.LEVEL, event.getLevel().toString()); message.setLongProperty(Constants.TIME_STAMP, event.getTimeStamp()); message.setStringProperty( Constants.EXCEPTION, StringUtils.join(event.getThrowableStrRep(), "\n")); message.setStringProperty(Constants.SERVICE, service); message.setStringProperty(Constants.ENVIRONMENT, environment); @SuppressWarnings("unchecked") Map<String, Object> context = MDC.getContext(); if (context != null) { for (String key : context.keySet()) { message.setStringProperty("context." + key, MDC.get(key).toString()); } } return message; }
public void producer(String msgData) throws JMSException { StompJmsConnectionFactory factory = new StompJmsConnectionFactory(); factory.setBrokerURI("tcp://" + apolloHost + ":" + apolloPort); System.out.println(factory.getBrokerURI()); Connection connection = factory.createConnection(apolloUser, apolloPassword); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = new StompJmsDestination(stompQueue); MessageProducer producer = session.createProducer(dest); System.out.println("Sending messages to " + stompQueue + "..."); TextMessage msg = session.createTextMessage(msgData); msg.setLongProperty("id", System.currentTimeMillis()); System.out.println(msg.getText()); producer.send(msg); // producer.send(session.createTextMessage("SHUTDOWN")); connection.close(); }
@Override public boolean offer(T event) throws FalconException { Session session; try { session = getSession(); TextMessage msg = session.createTextMessage(event.toString()); msg.setLongProperty( ScheduledMessage.AMQ_SCHEDULED_DELAY, event.getDelay(TimeUnit.MILLISECONDS)); msg.setStringProperty("TYPE", event.getType().name()); producer.send(msg); LOG.debug( "Enqueued Message: {} with delay {} milli sec", event.toString(), event.getDelay(TimeUnit.MILLISECONDS)); return true; } catch (Exception e) { LOG.error("Unable to offer event: {} to ActiveMQ", event, e); throw new FalconException("Unable to offer event:" + event + " to ActiveMQ", e); } }
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); java.io.PrintWriter out = res.getWriter(); // use a stringbuffer to avoid concatenation of Strings StringBuffer output = new StringBuffer(100); output.append( "<html><head><title>PingServlet2MDBQueue</title></head>" + "<body><HR><FONT size=\"+2\" color=\"#000066\">PingServlet2MDBQueue<BR></FONT>" + "<FONT size=\"-1\" color=\"#000066\">" + "Tests the basic operation of a servlet posting a message to an EJB MDB through a JMS Queue.<BR>" + "<FONT color=\"red\"><B>Note:</B> Not intended for performance testing.</FONT>"); try { if (queueConnectionFactory == null) { queueConnectionFactory = (ConnectionFactory) new InitialContext().lookup("ConnectionFactory"); } Connection conn = queueConnectionFactory.createConnection(); if (tradeBrokerQueue == null) { tradeBrokerQueue = (Queue) new InitialContext().lookup("java:comp/env/jms/TradeBrokerQueue"); } try { TextMessage message = null; int iter = TradeConfig.getPrimIterations(); for (int ii = 0; ii < iter; ii++) { Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); try { MessageProducer producer = sess.createProducer(tradeBrokerQueue); message = sess.createTextMessage(); String command = "ping"; message.setStringProperty("command", command); message.setLongProperty("publishTime", System.currentTimeMillis()); message.setText( "Ping message for queue java:comp/env/jms/TradeBrokerQueue sent from PingServlet2MDBQueue at " + new java.util.Date()); producer.send(message); } finally { sess.close(); } } // write out the output output.append("<HR>initTime: ").append(initTime); output.append("<BR>Hit Count: ").append(hitCount++); output.append("<HR>Posted Text message to java:comp/env/jms/TradeBrokerQueue destination"); output.append("<BR>Message: ").append(message); output.append("<BR><BR>Message text: ").append(message.getText()); output.append("<BR><HR></FONT></BODY></HTML>"); out.println(output.toString()); } catch (Exception e) { Log.error( "PingServlet2MDBQueue.doGet(...):exception posting message to TradeBrokerQueue destination "); throw e; } finally { conn.close(); } } // this is where I actually handle the exceptions catch (Exception e) { Log.error(e, "PingServlet2MDBQueue.doGet(...): error"); res.sendError(500, "PingServlet2MDBQueue.doGet(...): error, " + e.toString()); } }
private void propertiesPreserved(boolean persistent, boolean messageIDInHeader) throws Exception { BridgeImpl bridge = null; Connection connSource = null; Connection connTarget = null; try { final int NUM_MESSAGES = 10; bridge = new BridgeImpl( cff0, cff1, sourceQueueFactory, targetQueueFactory, null, null, null, null, null, 5000, 10, QualityOfServiceMode.AT_MOST_ONCE, 1, -1, null, null, messageIDInHeader); bridge.start(); connSource = cf0.createConnection(); connTarget = cf1.createConnection(); log.trace("Sending " + NUM_MESSAGES + " messages"); Session sessSource = connSource.createSession(false, Session.AUTO_ACKNOWLEDGE); Session sessTarget = connTarget.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer cons = sessTarget.createConsumer(targetQueue); connTarget.start(); MessageProducer prod = sessSource.createProducer(sourceQueue); prod.setDeliveryMode(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT); TextMessage tm = sessSource.createTextMessage("blahmessage"); prod.setPriority(7); prod.setTimeToLive(1 * 60 * 60 * 1000); prod.send(tm); long expiration = tm.getJMSExpiration(); assertEquals( persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT, tm.getJMSDeliveryMode()); tm = (TextMessage) cons.receive(1000); assertNotNull(tm); assertEquals("blahmessage", tm.getText()); assertEquals( persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT, tm.getJMSDeliveryMode()); assertEquals(7, tm.getJMSPriority()); assertTrue(Math.abs(expiration - tm.getJMSExpiration()) < 100); Message m = cons.receive(5000); assertNull(m); // Now do one with expiration = 0 tm = sessSource.createTextMessage("blahmessage2"); prod.setPriority(7); prod.setTimeToLive(0); prod.send(tm); assertEquals( persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT, tm.getJMSDeliveryMode()); tm = (TextMessage) cons.receive(1000); assertNotNull(tm); assertEquals("blahmessage2", tm.getText()); assertEquals( persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT, tm.getJMSDeliveryMode()); assertEquals(7, tm.getJMSPriority()); assertEquals(0, tm.getJMSExpiration()); m = cons.receive(5000); assertNull(m); tm = sessSource.createTextMessage("blahmessage3"); final boolean myBool = false; final byte myByte = (byte) 23; final double myDouble = 17625765d; final float myFloat = 87127.23f; final int myInt = 123; final long myLong = 81728712; final short myShort = (short) 88; final String myString = "ojweodewj"; final String myJMSX = "aardvark"; tm.setBooleanProperty("mybool", myBool); tm.setByteProperty("mybyte", myByte); tm.setDoubleProperty("mydouble", myDouble); tm.setFloatProperty("myfloat", myFloat); tm.setIntProperty("myint", myInt); tm.setLongProperty("mylong", myLong); tm.setShortProperty("myshort", myShort); tm.setStringProperty("mystring", myString); tm.setStringProperty("JMSXMyNaughtyJMSXProperty", myJMSX); prod.send(tm); tm = (TextMessage) cons.receive(1000); assertNotNull(tm); assertEquals("blahmessage3", tm.getText()); assertEquals(myBool, tm.getBooleanProperty("mybool")); assertEquals(myByte, tm.getByteProperty("mybyte")); assertEquals(myDouble, tm.getDoubleProperty("mydouble")); assertEquals(myFloat, tm.getFloatProperty("myfloat")); assertEquals(myInt, tm.getIntProperty("myint")); assertEquals(myLong, tm.getLongProperty("mylong")); assertEquals(myShort, tm.getShortProperty("myshort")); assertEquals(myString, tm.getStringProperty("mystring")); assertEquals(myJMSX, tm.getStringProperty("JMSXMyNaughtyJMSXProperty")); m = cons.receive(5000); } finally { if (bridge != null) { bridge.stop(); } if (connSource != null) { connSource.close(); } if (connTarget != null) { connTarget.close(); } } }