@Test public void testInvalidFactory_agent() throws Exception { // The internal client will be null. // But still, there will be no NPE or other exception. ReconfigurableClientAgent client = new ReconfigurableClientAgent(); client.setApplicationName( "app" ); client.setScopedInstancePath( "/root" ); client.switchMessagingType( null ); client.openConnection(); }
@Override public void sendMessageToTheDm( Message message ) throws IOException { // The context match the one used by the DM to listen to messages sent by agents. this.logger.fine( "Agent '" + getAgentId() + "' is sending a " + message.getClass().getSimpleName() + " message to the DM." ); MessagingContext ctx = new MessagingContext( RecipientKind.DM, this.domain, this.applicationName ); getMessagingClient().publish( ctx, message ); }
@Override public void listenToTheDm( ListenerCommand command ) throws IOException { listenToTheDm( getMessagingClient(), command ); }
@Override public void openConnection() throws IOException { openConnection( getMessagingClient()); }
@Override public void setApplicationName( String applicationName ) { this.applicationName = applicationName; // Propagate the information to the internal client. getMessagingClient().setOwnerProperties( getOwnerKind(), this.domain, applicationName, this.scopedInstancePath ); }
ReconfigurableClientAgent client1 = new ReconfigurableClientAgent(); client1.setRegistry( this.registry ); client1.associateMessageProcessor( createAgentProcessor( messages1 )); client1.setApplicationName( app.getName()); client1.setScopedInstancePath( "/" + instance1.getName()); client1.setExternalMapping( app.getExternalExports()); client1.switchMessagingType( getMessagingType()); this.clients.add( client1 ); ReconfigurableClientAgent client2 = new ReconfigurableClientAgent(); client2.setRegistry( this.registry ); client2.associateMessageProcessor( createAgentProcessor( messages2 )); client2.setApplicationName( app.getName()); client2.setScopedInstancePath( "/" + instance2.getName()); client2.setExternalMapping( app.getExternalExports()); client2.switchMessagingType( getMessagingType()); this.clients.add( client2 ); client1.requestExportsFromOtherAgents( instance1 ); Thread.sleep( getDelay()); Assert.assertEquals( 0, messages1.size()); client2.listenToRequestsFromOtherAgents( ListenerCommand.START, instance2 ); Thread.sleep( getDelay()); client1.requestExportsFromOtherAgents( instance1 ); Thread.sleep( getDelay()); client2.listenToRequestsFromOtherAgents( ListenerCommand.STOP, instance2 ); client1.listenToRequestsFromOtherAgents( ListenerCommand.START, instance1 );
ReconfigurableClientAgent agentClient_11 = new ReconfigurableClientAgent(); agentClient_11.setRegistry( this.registry ); agentClient_11.associateMessageProcessor( createAgentProcessor( agentMessages_11 )); agentClient_11.setApplicationName( app1.getName()); agentClient_11.setScopedInstancePath( "/" + app1_root1.getName()); agentClient_11.setExternalMapping( app1.getExternalExports()); agentClient_11.switchMessagingType( getMessagingType()); this.clients.add( agentClient_11 ); ReconfigurableClientAgent agentClient_12 = new ReconfigurableClientAgent(); agentClient_12.setRegistry( this.registry ); agentClient_12.associateMessageProcessor( createAgentProcessor( agentMessages_12 )); agentClient_12.setApplicationName( app1.getName()); agentClient_12.setScopedInstancePath( "/" + app1_root2.getName()); agentClient_12.setExternalMapping( app1.getExternalExports()); agentClient_12.switchMessagingType( getMessagingType()); this.clients.add( agentClient_12 ); ReconfigurableClientAgent agentClient_2 = new ReconfigurableClientAgent(); agentClient_2.setRegistry( this.registry ); agentClient_2.associateMessageProcessor( createAgentProcessor( agentMessages_2 )); agentClient_2.setApplicationName( app2.getName()); agentClient_2.setScopedInstancePath( "/" + app2_root.getName()); agentClient_2.setExternalMapping( app2.getExternalExports()); agentClient_2.switchMessagingType( getMessagingType()); this.clients.add( agentClient_2 ); agentClient_11.listenToTheDm( ListenerCommand.START ); agentClient_12.listenToTheDm( ListenerCommand.START );
final ReconfigurableClientAgent client = new ReconfigurableClientAgent(); client.console = Mockito.mock( PrintStream.class ); client.setApplicationName( "app" ); client.setScopedInstancePath( "/root" ); client.associateMessageProcessor( new AbstractMessageProcessor<IAgentClient>( "dummy.messageProcessor" ) { @Override protected void processMessage( final Message message ) { client.setRegistry(registry); Assert.assertNull( client.getMessagingType()); Assert.assertEquals( JmxWrapperForMessagingClient.class, client.getMessagingClient().getClass()); Assert.assertTrue(((JmxWrapperForMessagingClient) client.getMessagingClient()).isDismissClient()); Assert.assertSame( registry, client.getRegistry()); client.switchMessagingType( "foo" ); Assert.assertEquals( "foo", client.getMessagingType()); Assert.assertEquals( "foo", client.getMessagingClient().getMessagingType()); Assert.assertEquals( JmxWrapperForMessagingClient.class, client.getMessagingClient().getClass()); Assert.assertFalse(((JmxWrapperForMessagingClient) client.getMessagingClient()).isDismissClient()); (client.getMessagingClient()), "messagingClient", IMessagingClient.class ); client.switchMessagingType( "bar" ); Assert.assertEquals( "bar", client.getMessagingType()); Assert.assertEquals( "bar", client.getMessagingClient().getMessagingType());
this.messagingClient.setApplicationName( this.applicationName ); this.messagingClient.setScopedInstancePath( this.scopedInstancePath ); this.messagingClient.setIpAddress( this.ipAddress ); this.messagingClient.setNeedsModel( needsModel()); this.messagingClient.setDomain( this.domain ); this.messagingClient.switchMessagingType( this.messagingType);
@Test public void testAgent() throws Exception { // The messaging client is never null ReconfigurableClientAgent client = new ReconfigurableClientAgent(); Assert.assertNotNull( client.getMessagingClient()); // Invoke other method, no matter in which order client.setMessageQueue( new RoboconfMessageQueue()); Assert.assertFalse( client.hasValidClient()); Assert.assertFalse( client.isConnected()); }
if( this.messagingClient.isConnected()) { this.messagingClient.sendMessageToTheDm( new MsgNotifMachineDown( this.applicationName, this.scopedInstancePath )); this.logger.fine( "Agent " + getAgentId() + " notified the DM it was about to stop." ); this.messagingClient.getMessageProcessor().stopProcessor(); this.messagingClient.getMessageProcessor().interrupt(); this.messagingClient.closeConnection();
@Override public void publishExports( Instance instance ) throws IOException { // For all the exported variables... // ... find the component or facet name... Set<String> names = VariableHelpers.findPrefixesForExportedVariables( instance ); if( names.isEmpty()) this.logger.fine( "Agent '" + getAgentId() + "' is publishing its exports." ); else for( String facetOrComponentName : names ) { publishExports( instance, facetOrComponentName ); } }
@Override protected void openConnection( IMessagingClient newMessagingClient ) throws IOException { newMessagingClient.setOwnerProperties( getOwnerKind(), this.domain, this.applicationName, this.scopedInstancePath ); newMessagingClient.openConnection(); listenToTheDm( newMessagingClient, ListenerCommand.START ); MsgNotifHeartbeat msg = new MsgNotifHeartbeat( this.applicationName, this.scopedInstancePath, this.ipAddress ); msg.setModelRequired( this.needsModel ); MessagingContext ctx = new MessagingContext( RecipientKind.DM, this.domain, this.applicationName ); newMessagingClient.publish( ctx, msg ); }
@Override public void setMessageQueue( RoboconfMessageQueue messageQueue ) { getMessagingClient().setMessageQueue( messageQueue ); }
@Test public void testHasValidClient() { ReconfigurableClientAgent client = new ReconfigurableClientAgent(); Assert.assertFalse( client.hasValidClient()); }
/** * Starts the agent. * <p> * It is invoked by iPojo when an instance becomes VALID. * </p> */ public void start() { // Basic properties this.logger.info( "Agent '" + getAgentId() + "' is about to be launched." ); if( Utils.isEmptyOrWhitespaces( this.ipAddress )) { this.ipAddress = AgentUtils.findIpAddress( this.networkInterface ); this.logger.info( "IP address resolved to " + this.ipAddress ); } // Create a messaging client this.messagingClient = newReconfigurableClientAgent(); this.messagingClient.setDomain( this.domain ); AgentMessageProcessor messageProcessor = newMessageProcessor(); this.messagingClient.associateMessageProcessor( messageProcessor ); // Deal with dynamic parameters reloadUserData(); reconfigure(); // Prepare the timer for scheduled tasks TimerTask timerTask = new HeartbeatTask( this ); this.heartBeatTimer = new Timer( "Roboconf's Heartbeat Timer @ Agent", true ); this.heartBeatTimer.scheduleAtFixedRate( timerTask, Constants.HEARTBEAT_PERIOD, Constants.HEARTBEAT_PERIOD ); this.logger.info( "Agent '" + getAgentId() + "' was launched." ); }
/** * @return a new reconfigurable (messaging) client for the agent */ protected ReconfigurableClientAgent newReconfigurableClientAgent() { return new ReconfigurableClientAgent(); }
ReconfigurableClientAgent tomcatClient = new ReconfigurableClientAgent(); tomcatClient.setRegistry( this.registry ); tomcatClient.associateMessageProcessor( createAgentProcessor( tomcatMessages )); tomcatClient.setApplicationName( app1.getName()); tomcatClient.setScopedInstancePath( "/" + tomcat.getName()); tomcatClient.setExternalMapping( app1.getExternalExports()); tomcatClient.switchMessagingType( getMessagingType()); this.clients.add( tomcatClient ); ReconfigurableClientAgent apacheClient = new ReconfigurableClientAgent(); apacheClient.setRegistry( this.registry ); apacheClient.associateMessageProcessor( createAgentProcessor( apacheMessages )); apacheClient.setApplicationName( app1.getName()); apacheClient.setScopedInstancePath( "/" + apache.getName()); apacheClient.setExternalMapping( app1.getExternalExports()); apacheClient.switchMessagingType( getMessagingType()); this.clients.add( apacheClient ); ReconfigurableClientAgent mySqlClient = new ReconfigurableClientAgent(); mySqlClient.setRegistry( this.registry ); mySqlClient.associateMessageProcessor( createAgentProcessor( mySqlMessages )); mySqlClient.setApplicationName( app1.getName()); mySqlClient.setScopedInstancePath( "/" + mysql.getName()); mySqlClient.setExternalMapping( app1.getExternalExports()); mySqlClient.switchMessagingType( getMessagingType()); this.clients.add( mySqlClient ); ReconfigurableClientAgent otherClient = new ReconfigurableClientAgent(); otherClient.setRegistry( this.registry );
ReconfigurableClientAgent tomcatClient = new ReconfigurableClientAgent(); tomcatClient.setRegistry( this.registry ); tomcatClient.associateMessageProcessor( createAgentProcessor( tomcatMessages )); tomcatClient.setApplicationName( app1.getName()); tomcatClient.setScopedInstancePath( "/" + tomcat.getName()); tomcatClient.setExternalMapping( app1.getExternalExports()); tomcatClient.switchMessagingType( getMessagingType()); tomcatClient.listenToTheDm( ListenerCommand.START ); tomcatClient.listenToExportsFromOtherAgents( ListenerCommand.START, tomcat ); this.clients.add( tomcatClient ); ReconfigurableClientAgent apacheClient = new ReconfigurableClientAgent(); apacheClient.setRegistry( this.registry ); apacheClient.associateMessageProcessor( createAgentProcessor( apacheMessages )); apacheClient.setApplicationName( app1.getName()); apacheClient.setScopedInstancePath( "/" + apache.getName()); apacheClient.setExternalMapping( app1.getExternalExports()); apacheClient.switchMessagingType( getMessagingType()); apacheClient.listenToTheDm( ListenerCommand.START ); apacheClient.listenToExportsFromOtherAgents( ListenerCommand.START, apache ); this.clients.add( apacheClient ); ReconfigurableClientAgent mySqlClient = new ReconfigurableClientAgent(); mySqlClient.setRegistry( this.registry ); mySqlClient.associateMessageProcessor( createAgentProcessor( mySqlMessages )); mySqlClient.setApplicationName( app1.getName()); mySqlClient.setScopedInstancePath( "/" + mysql.getName()); mySqlClient.setExternalMapping( app1.getExternalExports()); mySqlClient.switchMessagingType( getMessagingType());
@Override public void setApplicationName( String applicationName ) { this.applicationName = applicationName; // Propagate the information to the internal client. getMessagingClient().setOwnerProperties( getOwnerKind(), this.domain, applicationName, this.scopedInstancePath ); }