use of javax.jms.ObjectMessage in project tomee by apache.
the class JmsTest method createListener.
private Destination createListener(final Connection connection) throws JMSException {
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the request Queue
final Destination requestQueue = session.createQueue(REQUEST_QUEUE_NAME);
final MessageConsumer consumer = session.createConsumer(requestQueue);
consumer.setMessageListener(new MessageListener() {
@Override
@SuppressWarnings("unchecked")
public void onMessage(final Message message) {
// if we got a dummy (non ObjectMessage) return
if (!(message instanceof ObjectMessage))
return;
MessageProducer producer = null;
try {
// process request
final ObjectMessage requestMessage = (ObjectMessage) message;
final Map<String, Object[]> request = (Map<String, Object[]>) requestMessage.getObject();
final Object[] args = request.get("args");
final String returnValue = "test-" + args[0];
// create response map
final Map<String, Object> response = new TreeMap<String, Object>();
response.put("return", returnValue);
// create response message
final ObjectMessage responseMessage = session.createObjectMessage();
responseMessage.setJMSCorrelationID(requestMessage.getJMSCorrelationID());
responseMessage.setObject((Serializable) response);
// send response message
producer = session.createProducer(requestMessage.getJMSReplyTo());
producer.send(responseMessage);
} catch (final Throwable e) {
e.printStackTrace();
} finally {
MdbUtil.close(producer);
}
}
});
return requestQueue;
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class MdbTest method createSender.
private void createSender() throws JMSException {
Connection connection = null;
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
connection = connectionFactory.createConnection();
connection.start();
// create request
final Map<String, Object> request = new TreeMap<String, Object>();
request.put("args", new Object[] { "cheese" });
// create a new temp response queue
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination responseQueue = session.createTemporaryQueue();
// Create a request messages
final ObjectMessage requestMessage = session.createObjectMessage();
requestMessage.setJMSReplyTo(responseQueue);
requestMessage.setObject((Serializable) request);
// Send the request message
producer = session.createProducer(session.createQueue(REQUEST_QUEUE_NAME));
producer.send(requestMessage);
// wait for the response message
consumer = session.createConsumer(responseQueue);
final Message message = consumer.receive(30000);
// verify message
assertNotNull("Did not get a response message", message);
assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
final ObjectMessage responseMessage = (ObjectMessage) message;
final Serializable object = responseMessage.getObject();
assertNotNull("Response ObjectMessage contains a null object");
assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
final Map<String, String> response = (Map<String, String>) object;
// process results
final String returnValue = (String) response.get("return");
assertEquals("test-cheese", returnValue);
} finally {
MdbUtil.close(consumer);
MdbUtil.close(producer);
MdbUtil.close(session);
MdbUtil.close(connection);
}
}
use of javax.jms.ObjectMessage in project tomee by apache.
the class MdbConnectionFactoryTests method test02_directRpc.
public void test02_directRpc() throws Exception {
final Connection connection = createConnection();
Session session = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
// create request
final Map<String, Object> request = new TreeMap<String, Object>();
request.put("method", "businessMethod(java.lang.String)");
request.put("args", new Object[] { "cheese" });
// initialize session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination requestQueue = session.createQueue("BasicMdb");
final Destination responseQueue = session.createTemporaryQueue();
// Create a request messages
final ObjectMessage requestMessage = session.createObjectMessage();
requestMessage.setJMSReplyTo(responseQueue);
requestMessage.setObject((Serializable) request);
// Send the request message
producer = session.createProducer(requestQueue);
producer.send(requestMessage);
// System.out.println("\n" + "***************************************\n" +
// "Sent request message: " + requestMessage + "\n" +
// " request map: " + request + "\n" +
// " to queue: " + requestQueue + "\n" +
// "***************************************\n\n");
// create consumer
consumer = session.createConsumer(responseQueue);
// System.out.println("\n" + "***************************************\n" +
// "Listening for response at : " + responseQueue + "\n" +
// "***************************************\n\n");
// wait for response mesage
final Message message = consumer.receive(1000);
// verify message
assertNotNull("Did not get a response message", message);
assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
final ObjectMessage responseMessage = (ObjectMessage) message;
final Serializable object = responseMessage.getObject();
assertNotNull("Response ObjectMessage contains a null object");
assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
final Map response = (Map) object;
// process results
if (response.containsKey("exception")) {
throw (Exception) response.get("return");
}
final String returnValue = (String) response.get("return");
assertEquals("eseehc", returnValue);
} finally {
MdbUtil.close(producer);
MdbUtil.close(session);
MdbUtil.close(connection);
}
}
use of javax.jms.ObjectMessage in project opennms by OpenNMS.
the class JmsNorthBounderTest method testObjectMessage.
@Test
public void testObjectMessage() throws Exception {
String xml = objectMessageConfigXml();
Resource resource = new ByteArrayResource(xml.getBytes());
JmsNorthbounderConfigDao dao = new JmsNorthbounderConfigDao();
dao.setConfigResource(resource);
dao.afterPropertiesSet();
JmsNorthbounderConfig config = dao.getConfig();
List<JmsDestination> destinations = config.getDestinations();
List<JmsNorthbounder> nbis = new LinkedList<>();
for (JmsDestination jmsDestination : destinations) {
JmsNorthbounder nbi = new JmsNorthbounder(config, m_jmsNorthbounderConnectionFactory, jmsDestination);
// nbi.setNodeDao(m_nodeDao);
nbi.afterPropertiesSet();
nbis.add(nbi);
}
List<NorthboundAlarm> alarms = new LinkedList<>();
OnmsNode node = new OnmsNode(null, NODE_LABEL);
node.setForeignSource("TestGroup");
node.setForeignId("2");
node.setId(m_nodeDao.getNextNodeId());
OnmsIpInterface ip = new OnmsIpInterface("127.0.0.1", node);
InetAddress ia = null;
try {
ia = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e) {
}
m_nodeDao.save(node);
m_nodeDao.flush();
// TX via NBIs
for (JmsNorthbounder nbi : nbis) {
OnmsEvent event = new OnmsEvent();
event.setId(5);
event.setEventUei("uei.uei.org/uei");
event.setEventTime(new Date());
event.setEventHost("eventhost");
event.setEventSource("eventsource");
event.setIpAddr(ia);
event.setDistPoller(null);
event.setEventSnmpHost("eventsnmphost");
event.setServiceType(null);
event.setEventSnmp("eventsnmp");
event.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event, "syslogmessage", "Dec 22 2015 20:12:57.1 UTC : %UC_CTI-3-CtiProviderOpenFailure: %[CTIconnectionId%61232238][ Login User Id%61pguser][Reason code.%61-1932787616][UNKNOWN_PARAMNAME:IPAddress%61172.17.12.73][UNKNOWN_PARAMNAME:IPv6Address%61][App ID%61Cisco CTIManager][Cluster ID%61SplkCluster][Node ID%61splkcucm6p]: CTI application failed to open provider%59 application startup failed", "string"), new OnmsEventParameter(event, "severity", "Error", "string"), new OnmsEventParameter(event, "timestamp", "Dec 22 14:13:21", "string"), new OnmsEventParameter(event, "process", "229250", "string"), new OnmsEventParameter(event, "service", "local7", "string")));
event.setEventCreateTime(new Date());
event.setEventDescr("eventdescr");
event.setEventLogGroup("eventloggroup");
event.setEventLogMsg("eventlogmsg");
event.setEventSeverity(4);
event.setEventPathOutage(null);
event.setEventCorrelation(null);
event.setEventSuppressedCount(0);
event.setEventOperInstruct("operinstruct");
event.setEventAutoAction(null);
event.setEventOperAction(null);
event.setEventOperActionMenuText(null);
event.setEventNotification(null);
event.setEventTTicket("tticketid");
event.setEventTTicketState(1);
event.setEventForward(null);
event.setEventMouseOverText(null);
event.setEventLog(null);
event.setEventDisplay(null);
event.setEventAckUser(null);
event.setEventAckTime(null);
event.setAlarm(null);
event.setNode(node);
event.setNotifications(null);
event.setAssociatedServiceRegainedOutages(null);
event.setAssociatedServiceLostOutages(null);
OnmsAlarm alarm = new OnmsAlarm(9, event.getEventUei(), null, 1, 4, new Date(), event);
alarm.setNode(node);
alarm.setDescription(event.getEventDescr());
alarm.setApplicationDN("applicationDN");
alarm.setLogMsg(event.getEventLogMsg());
alarm.setManagedObjectInstance("managedObjectInstance");
alarm.setManagedObjectType("managedObjectType");
alarm.setOssPrimaryKey("ossPrimaryKey");
alarm.setQosAlarmState("qosAlarmState");
alarm.setTTicketId("tticketId");
alarm.setReductionKey("reductionKey");
alarm.setClearKey("clearKey");
alarm.setOperInstruct("operInstruct");
alarm.setFirstEventTime(new Date(0));
alarm.setAlarmType(OnmsAlarm.PROBLEM_TYPE);
alarm.setIpAddr(ia);
alarm.setLastEvent(event);
alarm.setX733AlarmType(NorthboundAlarm.x733AlarmType.get(1).name());
alarm.setX733ProbableCause(NorthboundAlarm.x733ProbableCause.get(1).getId());
NorthboundAlarm a = new NorthboundAlarm(alarm);
alarms.add(a);
nbi.forwardAlarms(alarms);
}
Thread.sleep(100);
// Let's become a consumer and receive the message
Message m = m_template.receive("ObjectTestQueue");
Assert.assertNotNull(m);
Object response = ((ObjectMessage) m).getObject();
Assert.assertNotNull(response);
Assert.assertTrue("message\n'" + response + "'\n not a NorthboundAlarm\n'" + "'.", (response instanceof NorthboundAlarm));
String rk = ((NorthboundAlarm) response).getAlarmKey();
Assert.assertEquals("received alarm has incorrect reduction key: " + rk, "reductionKey", rk);
}
use of javax.jms.ObjectMessage in project spring-framework by spring-projects.
the class MessageListenerAdapterTests method testWithMessageContentsDelegateForObjectMessageWithPlainObject.
@Test
public void testWithMessageContentsDelegateForObjectMessageWithPlainObject() throws Exception {
ObjectMessage objectMessage = mock(ObjectMessage.class);
given(objectMessage.getObject()).willReturn(OBJECT);
MessageContentsDelegate delegate = mock(MessageContentsDelegate.class);
MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
adapter.onMessage(objectMessage);
verify(delegate).handleMessage(OBJECT);
}
Aggregations