Last active
October 23, 2019 17:41
-
-
Save c9katayama/4f921ff749778b37667633c83d671dee to your computer and use it in GitHub Desktop.
Function送る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//サーバ側 | |
import java.io.File; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
import java.nio.file.Files; | |
import java.nio.file.StandardCopyOption; | |
import java.util.function.Function; | |
import javax.servlet.ServletInputStream; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@SpringBootApplication | |
public class MyApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(MyApplication.class, args); | |
} | |
@RestController | |
public class IndexController { | |
@PostMapping("/invoke/{functionName}") | |
public int invoke(ServletInputStream is, @PathVariable String functionName) throws Exception { | |
File classDir = new File("myclasses"); | |
File classFile = new File(classDir, functionName.replace(".", "/") + ".class"); | |
classFile.getParentFile().mkdirs(); | |
Files.copy(is, classFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | |
URLClassLoader classLoader = new URLClassLoader(new URL[] { classDir.toURI().toURL() }); | |
int variable = 100; | |
return (Integer) ((Function) classLoader.loadClass(functionName).newInstance()).apply(variable); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//クライアント側 | |
import java.io.ByteArrayOutputStream; | |
import java.net.URL; | |
import org.apache.tomcat.util.http.fileupload.IOUtils; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.client.RestTemplate; | |
public class MyClient { | |
public static void main(String[] args) throws Exception { | |
Class<?> targetClass = MyFunction2.class; | |
String classFile = targetClass.getName().replace(".", "/").concat(".class"); | |
RestTemplate rest = new RestTemplate(); | |
URL classFileLocation = new URL( | |
targetClass.getProtectionDomain().getCodeSource().getLocation().toExternalForm() + classFile); | |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
IOUtils.copy(classFileLocation.openStream(), bout); | |
ResponseEntity<Integer> postForEntity = rest.postForEntity( | |
"http://localhost:8080/invoke/" + targetClass.getName(), bout.toByteArray(), Integer.class); | |
System.out.println(postForEntity.getBody());//足し算の結果 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//関数 | |
import java.util.function.Function; | |
public class MyFunction implements Function<Integer, Integer> { | |
@Override | |
public Integer apply(Integer t) { | |
return t + 100; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment