-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathSpringSampleAppApplication.java
More file actions
129 lines (108 loc) · 3.61 KB
/
Copy pathSpringSampleAppApplication.java
File metadata and controls
129 lines (108 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.io.Console;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import javax.sql.DataSource;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
@SpringBootApplication
public class SpringSampleAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSampleAppApplication.class, args);
}
}
@RestController
@RequestMapping("/")
@RefreshScope
@Component
class HomeRestController {
boolean healthy=true;
boolean alive=true;
String hostname="";
public HomeRestController(){
try {
hostname= "Hello from " + InetAddress.getLocalHost().getHostName().toString();
}
catch (UnknownHostException ex){
hostname= "error";
}
}
@RequestMapping("/")
public String home(){
return "<h1>"+hostname+"</h1>";
}
@RequestMapping("/ready")
public ResponseEntity ready(){
if (healthy)
return new ResponseEntity(HttpStatus.ACCEPTED);
else
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
}
@RequestMapping("/live")
public ResponseEntity live(){
if (alive)
return new ResponseEntity(HttpStatus.ACCEPTED);
else
return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
}
@RequestMapping("/infect")
public String infect(){
healthy=false;
return hostname+" is getting Sick ";
}
@RequestMapping("/cure")
public String cure(){
healthy=true;
return hostname+" is getting Better ";
}
@RequestMapping("/kill")
public String suffer(){
alive=false;
return hostname+" is Dying ";
}
@Autowired
private Environment env;
@RequestMapping("/dbtest")
public String dbtest(){
String sql = "SELECT * FROM customer";
Connection conn = null;
try {
//String connURL="jdbc:mysql://"+env.getProperty("MYSQL_SERVICE_HOST")+":"+env.getProperty("MYSQL_SERVICE_PORT")+"/"+env.getProperty("MYSQL_DATABASE")+"?useSSL=false";
//System.out.println("URL: "+connURL);
//conn = DriverManager.getConnection(connURL,env.getProperty("MYSQL_USER"),env.getProperty("MYSQL_PASSWORD"));
conn = DriverManager.getConnection(env.getProperty("spring.datasource.url"),env.getProperty("spring.datasource.username"),env.getProperty("spring.datasource.password"));
System.out.println("connection url: "+env.getProperty("spring.datasource.url"));
//System.out.println("Username: "+env.getProperty("spring.datasource.username")+"\nPassword: "+env.getProperty("spring.datasource.password"));
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
String res="<h1>Customers List</h1></br>";
while (rs.next()) {
res=res+"CustomerId: "+rs.getInt("CUST_ID") + " Customer Name: "+ rs.getString("NAME")+" Age: "+rs.getInt("Age")+"</br>";
}
rs.close();
return res;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
}