1616package de .codecentric .boot .admin .services ;
1717
1818import java .util .Collections ;
19+ import java .util .Map ;
1920import java .util .concurrent .atomic .AtomicReference ;
2021
2122import org .slf4j .Logger ;
3637 */
3738public class ApplicationRegistrator {
3839
39- private static final Logger LOGGER = LoggerFactory
40- .getLogger (ApplicationRegistrator .class );
40+ private static final Logger LOGGER = LoggerFactory .getLogger (ApplicationRegistrator .class );
4141
4242 private static HttpHeaders HTTP_HEADERS = createHttpHeaders ();
4343
44- private final AtomicReference <Application > registeredSelf = new AtomicReference <Application >();
44+ private final AtomicReference <String > registeredId = new AtomicReference <String >();
4545
4646 private AdminClientProperties client ;
4747
@@ -65,6 +65,7 @@ private static HttpHeaders createHttpHeaders() {
6565
6666 /**
6767 * Registers the client application at spring-boot-admin-server.
68+ *
6869 * @return true if successful
6970 */
7071 public boolean register () {
@@ -73,59 +74,51 @@ public boolean register() {
7374 try {
7475 self = createApplication ();
7576
76- ResponseEntity <Application > response = template .postForEntity (adminUrl ,
77- new HttpEntity <Application >(self , HTTP_HEADERS ), Application .class );
77+ @ SuppressWarnings ("rawtypes" )
78+ ResponseEntity <Map > response = template .postForEntity (adminUrl ,
79+ new HttpEntity <Application >(self , HTTP_HEADERS ), Map .class );
7880
7981 if (response .getStatusCode ().equals (HttpStatus .CREATED )) {
80- if (registeredSelf .get () == null ) {
81- if (registeredSelf .compareAndSet (null , response .getBody ())) {
82+ if (registeredId .get () == null ) {
83+ if (registeredId .compareAndSet (null , response .getBody (). get ( "id" ). toString ())) {
8284 LOGGER .info ("Application registered itself as {}" , response .getBody ());
8385 return true ;
8486 }
8587 }
8688
8789 LOGGER .debug ("Application refreshed itself as {}" , response .getBody ());
8890 return true ;
91+ } else {
92+ LOGGER .warn ("Application failed to registered itself as {}. Response: {}" , self ,
93+ response .toString ());
8994 }
90- else {
91- LOGGER .warn (
92- "Application failed to registered itself as {}. Response: {}" ,
93- self , response .toString ());
94- }
95- }
96- catch (Exception ex ) {
97- LOGGER .warn (
98- "Failed to register application as {} at spring-boot-admin ({}): {}" ,
99- self , adminUrl , ex .getMessage ());
95+ } catch (Exception ex ) {
96+ LOGGER .warn ("Failed to register application as {} at spring-boot-admin ({}): {}" , self ,
97+ adminUrl , ex .getMessage ());
10098 }
10199
102100 return false ;
103101 }
104102
105103 public void deregister () {
106- Application self = registeredSelf .get ();
107- if (self != null ) {
108- String adminUrl = admin .getUrl () + '/' + admin .getContextPath () + "/"
109- + self .getId ();
110-
111- registeredSelf .set (null );
104+ String id = registeredId .get ();
105+ if (id != null ) {
106+ String adminUrl = admin .getUrl () + '/' + admin .getContextPath () + "/" + id ;
112107
113108 try {
114109 template .delete (adminUrl );
115- }
116- catch (Exception ex ) {
110+ registeredId . set ( null );
111+ } catch (Exception ex ) {
117112 LOGGER .warn (
118- "Failed to deregister application as {} at spring-boot-admin ({}): {}" ,
119- self , adminUrl , ex .getMessage ());
113+ "Failed to deregister application (id={}) at spring-boot-admin ({}): {}" ,
114+ id , adminUrl , ex .getMessage ());
120115 }
121116 }
122117 }
123118
124119 protected Application createApplication () {
125- return Application .create (client .getName ())
126- .withHealthUrl (client .getHealthUrl ())
127- .withManagementUrl (client .getManagementUrl ())
128- .withServiceUrl (client .getServiceUrl ()).build ();
120+ return Application .create (client .getName ()).withHealthUrl (client .getHealthUrl ())
121+ .withManagementUrl (client .getManagementUrl ()).withServiceUrl (client .getServiceUrl ())
122+ .build ();
129123 }
130124}
131-
0 commit comments