/* * Initiate the snapshot by sending a Marker message to one of the Players (Player0) * Any Player could have been used to initiate the snapshot. */ private void sendInitSnapshot() { try { // Gather necessary JMS resources Context ctx = new InitialContext(); ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/myConnectionFactory"); Queue q = (Queue) ctx.lookup("jms/PITplayer0"); Connection con = cf.createConnection(); Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer writer = session.createProducer(q); /* * As part of the snapshot algorithm, players need to record * what other Players they receive markers from. * "-1" indicates to the PITplayer0 that this marker is coming from * the monitor, not another Player. */ Marker m = new Marker(-1); ObjectMessage msg = session.createObjectMessage(m); System.out.println("Initiating Snapshot"); writer.send(msg); con.close(); } catch (JMSException e) { System.out.println("JMS Exception thrown" + e); } catch (Throwable e) { System.out.println("Throwable thrown" + e); } }
@Override public void info(long lectureWrapperId) { InfoLectureWrapperDTO infoLectureWrapperDTO = new InfoLectureWrapperDTO(lectureWrapperId); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ObjectMessage objectMessage = session.createObjectMessage(infoLectureWrapperDTO); session.createProducer(serverQueue).send(objectMessage); } catch (JMSException e) { e.printStackTrace(); } }
public boolean send(@NotNull Value value, @Optional JMSQueue replyTo) throws JMSException { Message message = null; if (value.isArray()) { message = _session.createMapMessage(); ArrayValue array = (ArrayValue) value; Set<Map.Entry<Value, Value>> entrySet = array.entrySet(); for (Map.Entry<Value, Value> entry : entrySet) { if (entry.getValue() instanceof BinaryValue) { byte[] bytes = ((BinaryValue) entry.getValue()).toBytes(); ((MapMessage) message).setBytes(entry.getKey().toString(), bytes); } else { // every primitive except for bytes can be translated from a string ((MapMessage) message).setString(entry.getKey().toString(), entry.getValue().toString()); } } } else if (value instanceof BinaryValue) { message = _session.createBytesMessage(); byte[] bytes = ((BinaryValue) value).toBytes(); ((BytesMessage) message).writeBytes(bytes); } else if (value.isLongConvertible()) { message = _session.createStreamMessage(); ((StreamMessage) message).writeLong(value.toLong()); } else if (value.isDoubleConvertible()) { message = _session.createStreamMessage(); ((StreamMessage) message).writeDouble(value.toDouble()); } else if (value.toJavaObject() instanceof String) { message = _session.createTextMessage(); ((TextMessage) message).setText(value.toString()); } else if (value.toJavaObject() instanceof Serializable) { message = _session.createObjectMessage(); ((ObjectMessage) message).setObject((Serializable) value.toJavaObject()); } else { return false; } if (replyTo != null) message.setJMSReplyTo(replyTo._destination); _producer.send(message); return true; }
void sendMessage(String cmd, Map<String, String> paramMap) throws JMSException { Destination destination = session.createQueue(this.queueName); JSONObject json = new JSONObject(); json.put("cmd", cmd); for (String name : paramMap.keySet()) { json.put(name, paramMap.get(name)); } System.out.println( "sending user JMS message with payload:" + json.toString(2) + " to queue:" + this.queueName); Message message = session.createObjectMessage(json.toString()); this.producer.send(destination, message); }
public static void publish(String dest, Serializable object, String contentType, String tag) throws Exception { Connection conn = connectionFactory.createConnection(); try { Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = createDestination(dest); MessageProducer producer = session.createProducer(destination); ObjectMessage message = session.createObjectMessage(); if (contentType != null) { message.setStringProperty(HttpHeaderProperty.CONTENT_TYPE, contentType); } if (tag != null) { message.setStringProperty("MyTag", tag); } message.setObject(object); producer.send(message); } finally { conn.close(); } }
public static Message createMessage(Session session, Class messageClazz) throws JMSException { if (session == null) { throw new JMSException("session cannot be null"); } if (Message.class.isAssignableFrom(messageClazz)) { throw new JMSException("cannot create message of type: " + messageClazz.getName()); } if (TextMessage.class.equals(messageClazz)) { return session.createTextMessage(); } else if (StreamMessage.class.equals(messageClazz)) { return session.createStreamMessage(); } else if (MapMessage.class.equals(messageClazz)) { return session.createMapMessage(); } else if (ObjectMessage.class.equals(messageClazz)) { return session.createObjectMessage(); } else if (BytesMessage.class.equals(messageClazz)) { return session.createBytesMessage(); } else if (Message.class.equals(messageClazz)) { return session.createMessage(); } else { throw new JMSException("cannot create message of type: " + messageClazz.getName()); } }