/** * If the supplied managed resource implements the {@link NotificationPublisherAware} an instance of * {@link org.springframework.jmx.export.notification.NotificationPublisher} is injected. */ private void injectNotificationPublisherIfNecessary( Object managedResource, @Nullable ModelMBean modelMBean, @Nullable ObjectName objectName) { if (managedResource instanceof NotificationPublisherAware && modelMBean != null && objectName != null) { ((NotificationPublisherAware) managedResource).setNotificationPublisher( new ModelMBeanNotificationPublisher(modelMBean, objectName, managedResource)); } }
/** * Send the supplied {@link Notification} using the wrapped * {@link ModelMBean} instance. * @param notification the {@link Notification} to be sent * @throws IllegalArgumentException if the supplied {@code notification} is {@code null} * @throws UnableToSendNotificationException if the supplied {@code notification} could not be sent */ @Override public void sendNotification(Notification notification) { Assert.notNull(notification, "Notification must not be null"); replaceNotificationSourceIfNecessary(notification); try { if (notification instanceof AttributeChangeNotification) { this.modelMBean.sendAttributeChangeNotification((AttributeChangeNotification) notification); } else { this.modelMBean.sendNotification(notification); } } catch (MBeanException ex) { throw new UnableToSendNotificationException("Unable to send notification [" + notification + "]", ex); } }
@Test(expected = IllegalArgumentException.class) public void testCtorWithNullMBean() throws Exception { new ModelMBeanNotificationPublisher(null, createObjectName(), this); }
public void testSendVanillaNotification() throws Exception { StubSpringModelMBean mbean = new StubSpringModelMBean(); Notification notification = new Notification("network.alarm.router", mbean, 1872); ObjectName objectName = createObjectName(); NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean); publisher.sendNotification(notification); assertNotNull(mbean.getActualNotification()); assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification()); assertSame("The 'source' property of the Notification is not being set to the ObjectName of the associated MBean.", objectName, mbean.getActualNotification().getSource()); }
public void sendRequestTime(String urlGroup, long time) { if(notificationPublisher != null) { Notification note = new Notification(urlGroup, time, 0); notificationPublisher.sendNotification(note); } else { logger.debug("notification publisher not found by UaaMetricsFilter"); } }
@Test(expected = IllegalArgumentException.class) public void testSendNullNotification() throws Exception { NotificationPublisher publisher = new ModelMBeanNotificationPublisher(new SpringModelMBean(), createObjectName(), this); publisher.sendNotification(null); }
@Test(expected = IllegalArgumentException.class) public void testCtorWithNullObjectName() throws Exception { new ModelMBeanNotificationPublisher(new SpringModelMBean(), null, this); }
public void testSendAttributeChangeNotification() throws Exception { StubSpringModelMBean mbean = new StubSpringModelMBean(); Notification notification = new AttributeChangeNotification(mbean, 1872, System.currentTimeMillis(), "Shall we break for some tea?", "agree", "java.lang.Boolean", Boolean.FALSE, Boolean.TRUE); ObjectName objectName = createObjectName(); NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean); publisher.sendNotification(notification); assertNotNull(mbean.getActualNotification()); assertTrue(mbean.getActualNotification() instanceof AttributeChangeNotification); assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification()); assertSame("The 'source' property of the Notification is not being set to the ObjectName of the associated MBean.", objectName, mbean.getActualNotification().getSource()); }
public void sendNotification() { this.notificationPublisher.sendNotification(new Notification("test", this, 1)); }
/** * If the supplied managed resource implements the {@link NotificationPublisherAware} an instance of * {@link org.springframework.jmx.export.notification.NotificationPublisher} is injected. */ private void injectNotificationPublisherIfNecessary( Object managedResource, @Nullable ModelMBean modelMBean, @Nullable ObjectName objectName) { if (managedResource instanceof NotificationPublisherAware && modelMBean != null && objectName != null) { ((NotificationPublisherAware) managedResource).setNotificationPublisher( new ModelMBeanNotificationPublisher(modelMBean, objectName, managedResource)); } }
/** * Send the supplied {@link Notification} using the wrapped * {@link ModelMBean} instance. * @param notification the {@link Notification} to be sent * @throws IllegalArgumentException if the supplied {@code notification} is {@code null} * @throws UnableToSendNotificationException if the supplied {@code notification} could not be sent */ @Override public void sendNotification(Notification notification) { Assert.notNull(notification, "Notification must not be null"); replaceNotificationSourceIfNecessary(notification); try { if (notification instanceof AttributeChangeNotification) { this.modelMBean.sendAttributeChangeNotification((AttributeChangeNotification) notification); } else { this.modelMBean.sendNotification(notification); } } catch (MBeanException ex) { throw new UnableToSendNotificationException("Unable to send notification [" + notification + "]", ex); } }
@Test(expected = IllegalArgumentException.class) public void testCtorWithNullManagedResource() throws Exception { new ModelMBeanNotificationPublisher(new SpringModelMBean(), createObjectName(), null); }
public void testSendAttributeChangeNotificationWhereSourceIsNotTheManagedResource() throws Exception { StubSpringModelMBean mbean = new StubSpringModelMBean(); Notification notification = new AttributeChangeNotification(this, 1872, System.currentTimeMillis(), "Shall we break for some tea?", "agree", "java.lang.Boolean", Boolean.FALSE, Boolean.TRUE); ObjectName objectName = createObjectName(); NotificationPublisher publisher = new ModelMBeanNotificationPublisher(mbean, objectName, mbean); publisher.sendNotification(notification); assertNotNull(mbean.getActualNotification()); assertTrue(mbean.getActualNotification() instanceof AttributeChangeNotification); assertSame("The exact same Notification is not being passed through from the publisher to the mbean.", notification, mbean.getActualNotification()); assertSame("The 'source' property of the Notification is *wrongly* being set to the ObjectName of the associated MBean.", this, mbean.getActualNotification().getSource()); }
/** * Publish the provided message to an external listener if there is one. * * @param message the message to publish */ private void publish(String message) { if (notificationPublisher != null) { Notification notification = new Notification("JobExecutionApplicationEvent", this, notificationCount++, message); /* * We can't create a notification with a null source, but we can set * it to null after creation(!). We want it to be null so that * Spring will replace it automatically with the ObjectName (in * ModelMBeanNotificationPublisher). */ notification.setSource(null); notificationPublisher.sendNotification(notification); } } }
/** * Publish the provided message to an external listener if there is one. * * @param message the message to publish */ private void publish(String message) { if (notificationPublisher != null) { Notification notification = new Notification("JobExecutionApplicationEvent", this, notificationCount++, message); /* * We can't create a notification with a null source, but we can set * it to null after creation(!). We want it to be null so that * Spring will replace it automatically with the ObjectName (in * ModelMBeanNotificationPublisher). */ notification.setSource(null); notificationPublisher.sendNotification(notification); } }
private void publish(Notification notification) { Assert.state(this.notificationPublisher != null, "NotificationPublisher must not be null."); this.notificationPublisher.sendNotification(notification); }
@Test public void happy_path() throws Exception { filter.setPerRequestMetrics(true); String path = performTwoSimpleRequests(); Map<String, String> summary = filter.getSummary(); assertNotNull(summary); assertFalse(summary.isEmpty()); assertEquals(2, summary.size()); for (String uri : Arrays.asList(path, MetricsUtil.GLOBAL_GROUP)) { MetricsQueue totals = readValue(summary.get(filter.getUriGroup(request).getGroup()), MetricsQueue.class); assertNotNull("URI:"+uri, totals); for (StatusCodeGroup status : Arrays.asList(StatusCodeGroup.SUCCESS, StatusCodeGroup.SERVER_ERROR)) { RequestMetricSummary total = totals.getDetailed().get(status); assertEquals("URI:"+uri, 1, total.getCount()); } } assertNull(MetricsAccessor.getCurrent()); ArgumentCaptor<Notification> argumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(publisher, times(2)).sendNotification(argumentCaptor.capture()); List<Notification> capturedArg = argumentCaptor.getAllValues(); assertEquals(2, capturedArg.size()); assertEquals("/api" , capturedArg.get(0).getType()); }
public void send(String s) { Notification notification = new Notification("test.type", "org.springframework.integration.jmx.config:type=TestPublisher,name=" + this.beanName, 1, s); this.notificationPublisher.sendNotification(notification); }
@Test public void intolerable_request() throws Exception { TimeService slowRequestTimeService = new TimeService() { long now = System.currentTimeMillis(); @Override public long getCurrentTimeMillis() { now += 5000; return now; } }; for (TimeService timeService : Arrays.asList(slowRequestTimeService, new TimeServiceImpl())) { reset(publisher); filter = new UaaMetricsFilter(); filter.setPerRequestMetrics(true); filter.setTimeService(timeService); filter.setNotificationPublisher(publisher); String path = "/authenticate/test"; setRequestData(path); filter.getUriGroup(request).setLimit(1000); filter.doFilterInternal(request, response, chain); MetricsQueue metricsQueue = filter.getMetricsQueue(filter.getUriGroup(request).getGroup()); RequestMetricSummary totals = metricsQueue.getTotals(); assertEquals(1, totals.getCount()); assertEquals(timeService == slowRequestTimeService ? 1 : 0, totals.getIntolerableCount()); ArgumentCaptor<Notification> argumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(publisher).sendNotification(argumentCaptor.capture()); Notification capturedArg = argumentCaptor.getValue(); assertEquals("/api" , capturedArg.getType()); } }
public void publish(String message) { Notification notification = new Notification("testType", this, sequence.getAndIncrement(), message); this.notificationPublisher.sendNotification(notification); }