Skip to content

Commit 2405320

Browse files
committed
Adding project files
1 parent bacb49d commit 2405320

18 files changed

+382
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# SpringBootPythonTest
1+
# SpringBootPythonTest
2+
3+
This is a spring boot project without an embedded tomcat.
4+
So, to run the project, this needs to deployed over a external tomcat.
5+
6+
E.g. :
7+
- Can be deployed to standalone tomcat installed in os
8+
- Can be copied to Tomcat based docker image and run the image for bringing up the application
9+
10+
Resources :
11+
api.py - A python script to expose a api using Flask
12+
pyStart.py - python script to log data to log file and launch perm.sh
13+
perm.sh - shell script to set the port of tomcat to 8888, assign executable permissions to pyStart.py script, run api.py using nohup to run it in background
14+
nohup.out - Automatically generated, whenever nohup is invoked
15+
helloApi-0.0.1-SNAPSHOT.jar - An simple spring boot application which exposes an helloWorld api
16+
17+
Imp points :
18+
The Main Application extends SpringBootServletInitializer class and overrides configure method, so that this application can be deployed over external tomcat as war
19+
Init Method in main class is to launch scripts inside docker image
20+

pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.sky</groupId>
12+
<artifactId>test1</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
<name>test1</name>
16+
<description>Demo project for Spring Boot</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
<exclusions>
33+
<exclusion>
34+
<groupId>org.junit.vintage</groupId>
35+
<artifactId>junit-vintage-engine</artifactId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.apache.commons</groupId>
42+
<artifactId>commons-exec</artifactId>
43+
<version>1.3</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-tomcat</artifactId>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>

pythonExecution.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2019-12-03 23:44:06,409 Inside init.....
2+
2019-12-03 23:44:06,410 2019-12-03 23:44:06.409985

src/.DS_Store

6 KB
Binary file not shown.

src/main/.DS_Store

6 KB
Binary file not shown.

src/main/java/.DS_Store

6 KB
Binary file not shown.

src/main/java/com/.DS_Store

6 KB
Binary file not shown.

src/main/java/com/sky/.DS_Store

6 KB
Binary file not shown.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.sky.test1;
2+
3+
import com.sky.test1.controller.Test1Controller;
4+
import org.apache.commons.exec.CommandLine;
5+
import org.apache.commons.exec.DefaultExecutor;
6+
import org.apache.commons.exec.ExecuteException;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.boot.builder.SpringApplicationBuilder;
12+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
13+
14+
import javax.annotation.PostConstruct;
15+
import java.io.BufferedReader;
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.io.InputStreamReader;
19+
20+
/*
21+
* Application to invoke python script from Java
22+
* */
23+
@SpringBootApplication
24+
public class Test1Application extends SpringBootServletInitializer {
25+
26+
private static final Logger logger = LoggerFactory.getLogger(Test1Controller.class);
27+
28+
private static Process p = null;
29+
30+
public static void main(String[] args) {
31+
SpringApplication.run(Test1Application.class, args);
32+
}
33+
34+
@Override
35+
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
36+
return builder.sources(Test1Application.class);
37+
}
38+
39+
/*@PostConstruct
40+
public void init() throws IOException {
41+
logger.info("Test1Application - Inside Post Construct");
42+
System.out.println("Inside post construct \n Application started, now invoking python script");
43+
logger.info("Test1Application - Application started, now invoking python script.......");
44+
if(p == null) {
45+
ClassLoader classLoader = getClass().getClassLoader();
46+
File file = new File(classLoader.getResource("api.py").getFile());
47+
String fileName = file.getAbsolutePath();
48+
logger.info("Test1Application - "+fileName);
49+
50+
String[] cmd = {
51+
"/bin/bash",
52+
"-c",
53+
"python3 "+ fileName
54+
};
55+
p = Runtime.getRuntime().exec(cmd);
56+
//p.waitFor();
57+
}
58+
else
59+
logger.info("TestApplication Python process already running...");
60+
61+
logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct");
62+
}*/
63+
64+
/*@PostConstruct
65+
public void init() throws IOException, InterruptedException {
66+
logger.info("Test1Application - Inside Post Construct");
67+
System.out.println("Inside post construct \n Application started, now invoking python script");
68+
logger.info("Test1Application - Application started, now invoking python script.......");
69+
if(p == null) {
70+
ClassLoader classLoader = getClass().getClassLoader();
71+
*//*File file = new File(classLoader.getResource("api.py").getFile());
72+
String fileName = file.getAbsolutePath();
73+
logger.info("Test1Application - "+fileName);*//*
74+
75+
File file2 = new File(classLoader.getResource("perm.sh").getFile());
76+
File parentDir = file2.getParentFile();
77+
System.out.println(parentDir);
78+
String initFileName = file2.getAbsolutePath();
79+
logger.info("Test1Application - "+initFileName);
80+
81+
String[] cmd = {
82+
"/bin/bash",
83+
"-c",
84+
"cd "+parentDir,
85+
"./ "+initFileName
86+
};
87+
88+
//String cmd = "python3 "+initFileName;
89+
p = Runtime.getRuntime().exec(cmd);
90+
p.waitFor();
91+
String line;
92+
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
93+
while ((line = input.readLine()) != null) {
94+
System.out.println(line);
95+
}
96+
input.close();
97+
}
98+
else
99+
logger.info("TestApplication Python process already running...");
100+
101+
logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct");
102+
}*/
103+
104+
/*@PostConstruct
105+
public void init() throws IOException, InterruptedException {
106+
logger.info("Test1Application - Inside Post Construct");
107+
System.out.println("Inside post construct \n Application started, now invoking python script");
108+
logger.info("Test1Application - Application started, now invoking python script.......");
109+
if(p == null) {
110+
ClassLoader classLoader = getClass().getClassLoader();
111+
*//*File file = new File(classLoader.getResource("api.py").getFile());
112+
String fileName = file.getAbsolutePath();
113+
logger.info("Test1Application - "+fileName);*//*
114+
115+
File file2 = new File(classLoader.getResource("api.py").getFile());
116+
File parentDir = file2.getParentFile();
117+
System.out.println(parentDir);
118+
String initFileName = file2.getAbsolutePath();
119+
logger.info("Test1Application - "+initFileName);
120+
121+
ProcessBuilder processBuilder = new ProcessBuilder();
122+
//processBuilder.command("bash", "-c", "ls /Users/sky/");
123+
processBuilder.command("bash", "-c", "python3 "+initFileName);
124+
125+
try {
126+
127+
Process process = processBuilder.start();
128+
129+
StringBuilder output = new StringBuilder();
130+
131+
BufferedReader reader = new BufferedReader(
132+
new InputStreamReader(process.getInputStream()));
133+
134+
String line;
135+
while ((line = reader.readLine()) != null) {
136+
output.append(line + "\n");
137+
}
138+
139+
int exitVal = process.waitFor();
140+
if (exitVal == 0) {
141+
System.out.println("Success!");
142+
System.out.println(output);
143+
System.exit(0);
144+
} else {
145+
System.out.println("abnormal......");
146+
}
147+
148+
} catch (IOException e) {
149+
e.printStackTrace();
150+
} catch (InterruptedException e) {
151+
e.printStackTrace();
152+
}
153+
}
154+
else
155+
logger.info("TestApplication Python process already running...");
156+
157+
logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct");
158+
}*/
159+
160+
@PostConstruct
161+
public void init() {
162+
logger.info("Test1Application - Inside Post Construct");
163+
System.out.println("Inside post construct \n Application started, now invoking python script");
164+
logger.info("Test1Application - Application started, now invoking python script.......");
165+
if(p == null) {
166+
ClassLoader classLoader = getClass().getClassLoader();
167+
/*File file = new File(classLoader.getResource("api.py").getFile());
168+
String fileName = file.getAbsolutePath();
169+
logger.info("Test1Application - "+fileName);*/
170+
171+
File file2 = new File(classLoader.getResource("perm.sh").getFile());
172+
File parentDir = file2.getParentFile();
173+
System.out.println(parentDir);
174+
String initFileName = file2.getAbsolutePath();
175+
logger.info("Test1Application - "+initFileName);
176+
177+
String sCommandString = "sh "+initFileName;
178+
CommandLine oCmdLine = CommandLine.parse(sCommandString);
179+
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
180+
oDefaultExecutor.setExitValue(0);
181+
182+
Thread commandLineThread = new Thread(() -> {
183+
try {
184+
int iExitValue = oDefaultExecutor.execute(oCmdLine);
185+
System.out.println("-------" + iExitValue);
186+
} catch (ExecuteException e) {
187+
System.err.println("Execution failed.");
188+
e.printStackTrace();
189+
} catch (IOException e) {
190+
System.err.println("permission denied.");
191+
e.printStackTrace();
192+
}
193+
});
194+
commandLineThread.setDaemon(true);
195+
commandLineThread.start();
196+
197+
}
198+
else
199+
logger.info("TestApplication Python process already running...");
200+
201+
logger.info("Test1Application - After invoking python script.......\n Exiting Post Construct");
202+
}
203+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.sky.test1.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping("/")
11+
public class Test1Controller {
12+
13+
private static final Logger logger = LoggerFactory.getLogger(Test1Controller.class);
14+
15+
@GetMapping("/hello")
16+
public String helloWorld() {
17+
System.out.println("hello invoked");
18+
logger.info("Test1Controller - hello invoked");
19+
return "Hello World";
20+
}
21+
}

src/main/resources/.DS_Store

6 KB
Binary file not shown.

src/main/resources/api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route("/")
7+
def hello():
8+
print("inside python hello")
9+
return "Python Api is running..."
10+
11+
12+
app.run()
13+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
logging.file=${catalina.home}logs/application.log
16.8 MB
Binary file not shown.

src/main/resources/nohup.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
* Serving Flask app "api" (lazy loading)
2+
* Environment: production
3+
WARNING: This is a development server. Do not use it in a production deployment.
4+
Use a production WSGI server instead.
5+
* Debug mode: off
6+
Traceback (most recent call last):
7+
File "/Users/sky/Desktop/tomcat/apache-tomcat-8.5.49/webapps/test1-0.0.1-SNAPSHOT/WEB-INF/classes/api.py", line 12, in <module>
8+
app.run()
9+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 990, in run
10+
run_simple(host, port, self, **options)
11+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/werkzeug/serving.py", line 1010, in run_simple
12+
inner()
13+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/werkzeug/serving.py", line 954, in inner
14+
srv = make_server(
15+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/werkzeug/serving.py", line 805, in make_server
16+
return ThreadedWSGIServer(
17+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/werkzeug/serving.py", line 699, in __init__
18+
HTTPServer.__init__(self, server_address, handler)
19+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 452, in __init__
20+
self.server_bind()
21+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/server.py", line 137, in server_bind
22+
socketserver.TCPServer.server_bind(self)
23+
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socketserver.py", line 466, in server_bind
24+
self.socket.bind(self.server_address)
25+
OSError: [Errno 48] Address already in use

src/main/resources/perm.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
echo "perm start"
4+
java -Dserver.port=8888 -jar /Users/sky/Desktop/tomcat/apache-tomcat-8.5.49/webapps/test1-0.0.1-SNAPSHOT/WEB-INF/classes/helloApi-0.0.1-SNAPSHOT.jar
5+
chmod 777 /Users/sky/Desktop/tomcat/apache-tomcat-8.5.49/webapps/test1-0.0.1-SNAPSHOT/WEB-INF/classes/pyStart.py
6+
nohup python3 /Users/sky/Desktop/tomcat/apache-tomcat-8.5.49/webapps/test1-0.0.1-SNAPSHOT/WEB-INF/classes/api.py
7+
echo "perm end"

src/main/resources/pyStart.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subprocess
2+
import os
3+
from datetime import datetime
4+
import logging
5+
import shlex
6+
7+
# Create and configure logger
8+
logging.basicConfig(filename="pythonExecution.log",
9+
format='%(asctime)s %(message)s',
10+
filemode='w')
11+
12+
# Creating an object
13+
logger = logging.getLogger()
14+
15+
# Setting the threshold of logger to DEBUG
16+
logger.setLevel(logging.INFO)
17+
18+
19+
logger.info("Inside init.....")
20+
logger.info(datetime.now())
21+
os.chmod("/Users/sky/Desktop/java_code/test1/target/classes/perm.sh", 0o775)
22+
subprocess.call(shlex.split('sh perm.sh'))
23+
#subprocess.call('./run_cmd.sh')
24+
logger.info("End of init script")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2019-12-04 23:18:14,127 Inside init.....
2+
2019-12-04 23:18:14,127 2019-12-04 23:18:14.127521
3+
2019-12-04 23:18:14,423 End of init script

0 commit comments

Comments
 (0)