I have an akka (akka-actor_2.11) application that we use for stress testing one of our systems. The top level actor called RunCoordinatorActor is able to know based on responses coming from its subordinates when the work is finished.
When the work is finished the RunCoordinatorActor makes a call to getContext().system().shutdown() and then in the main method there is a loop checking for the system.isTerminated() call to return true. All works fine and I am happy with the way it works. However both system.sutdown() and system.isTerminated() methods are marked as deprecated and I am trying to figure out the right way to implement a graceful shutdown without using them.
Here is my main class:
public static void main(String[] args) throws Exception {
if (new ArgumentsValidator().validate(args)) {
// If the arguments are valid then we can load spring application
// context on here.
final ApplicationContext context = new AnnotationConfigApplicationContext(
M6ApplicationContext.class);
// Use an akka system to be able to send messages in parallel
// without doing the low level thread manipulation ourselves.
final ActorSystem system = context.getBean(ActorSystem.class);
final ActorRef runCoordinator = system.actorOf(SPRING_EXT_PROVIDER.get(system)
.props("RunCoordinatorActor"), "runCoordinator");
Thread.sleep(1000);
runCoordinator.tell(new StartTesting(), ActorRef.noSender());
do {
LOGGER.info("Waiting for the process to finish");
Thread.sleep(60000L);
// What would be the alternative for isTerminated() code below
} while (!system.isTerminated());
}
}
and here is my call to shutdown inside the RunCoordinator class:
#Named("RunCoordinatorActor")
#Scope("prototype")
public class RunCoordinator extends UntypedActor {
#Override
public void onReceive(Object message) throws Exception {
....
if (message instanceof WorkDone) {
getContext().system().shutdown();
}
}
}
I can see there is another method called terminate() that returns a Future and if I replace the shutdown call with that it all works OK too.
if (message instanceof WorkDone) {
Future<Terminated> work = getContext().system().terminate();
// But where should I put the call work.isCompleted()
// and how would I make the main aware of it
}
I could find some scala examples on here shutdown-patterns-in-akka-2 but they still use system.shutdown in the end so not sure how up to date that post still is.
Thank you in advance for your inputs.
The solution was not that hard to find once I looked closer into the ActorSystem API.
All I had to do was to add this to my RunCoordinator class:
if (message instanceof WorkDone) {
getContext().system().terminate();
}
And had a Future<Terminated> workDone = system.whenTerminated(); defined in my main class which after the change became:
public static void main(String[] args) throws Exception {
if (new ArgumentsValidator().validate(args)) {
// If the arguments are valid then we can load spring application
// context on here.
final ApplicationContext context = new AnnotationConfigApplicationContext(
M6ApplicationContext.class);
// Use an akka system to be able to send messages in parallel
// without doing the low level thread manipulation ourselves.
final ActorSystem system = context.getBean(ActorSystem.class);
final Future<Terminated> workDone = system.whenTerminated();
final ActorRef runCoordinator = system.actorOf(SPRING_EXT_PROVIDER.get(system)
.props("RunCoordinatorActor"), "runCoordinator");
runCoordinator.tell(new StartTesting(), ActorRef.noSender());
do {
LOGGER.info("Waiting for the process to finish");
Thread.sleep(60000L);
} while (!workDone.isCompleted());
}
}
All worked very well after this. I am still surprised google cold not take me to any existing example showing how to do it.
Related
I am learning how to write Minecraft mods (version 1.14.4) and was able to make an item. Now I am trying to make a block. I am following this tutorial video which actually covers 1.14.3, but I thought it would be close enough.
I am currently getting this error:
[20Mar2020 14:09:10.522] [Server thread/INFO] [net.minecraftforge.registries.ForgeRegistry/REGISTRIES]: Registry Block: Found a missing id from the world examplemod:examplemod
[20Mar2020 14:09:10.613] [Server thread/ERROR] [net.minecraftforge.registries.GameData/REGISTRIES]: Unidentified mapping from registry minecraft:block
examplemod:examplemod: 676
I also get presented with this at runtime:
I have tried messing around with how i'm naming the registries but I just can't seem to pin down what the issue is. Maybe i'm not formatting my json files correctly?
Note that ItemList and BlockList are just classes that contain static instances of each Block/Item I have created.
ExampleMod.java:
// The value here should match an entry in the META-INF/mods.toml file
#Mod(ExampleMod.MOD_ID)
public class ExampleMod
{
// Directly reference a log4j logger.
private static final Logger LOGGER = LogManager.getLogger();
public static final String MOD_ID = "examplemod";
public ExampleMod() {
// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}
private void setup(final FMLCommonSetupEvent event)
{
// some preinit code
LOGGER.info("HELLO FROM PREINIT");
LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
}
private void doClientStuff(final FMLClientSetupEvent event) {
// do something that can only be done on the client
LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
}
private void enqueueIMC(final InterModEnqueueEvent event)
{
// some example code to dispatch IMC to another mod
InterModComms.sendTo(ExampleMod.MOD_ID, "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
}
private void processIMC(final InterModProcessEvent event)
{
// some example code to receive and process InterModComms from other mods
LOGGER.info("Got IMC {}", event.getIMCStream().
map(m->m.getMessageSupplier().get()).
collect(Collectors.toList()));
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
#SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {
// do something when the server starts
LOGGER.info("HELLO from server starting");
}
// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
// Event bus for receiving Registry Events)
#Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {
#SubscribeEvent
public static void onItemsRegistry(final RegistryEvent.Register<Item> blockItemEvent)
{
ItemList.bomb_item = new Item(new Item.Properties().group(ItemGroup.COMBAT));
ItemList.bomb_item.setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "bomb_item"));
ItemList.mystery_block = new BlockItem(BlockList.mystery_block, new Item.Properties().group(ItemGroup.MISC));
ItemList.mystery_block.setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "mystery_block"));
blockItemEvent.getRegistry().registerAll(ItemList.bomb_item, ItemList.mystery_block);
LOGGER.info("Items registered.");
}
#SubscribeEvent
public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
BlockList.mystery_block = new Block(Block.Properties.create(Material.CAKE)
.hardnessAndResistance(2.0f, 2.0f)
.sound(SoundType.GLASS));
BlockList.mystery_block.setRegistryName(new ResourceLocation(MOD_ID, "mystery_block"));
blockRegistryEvent.getRegistry().registerAll(BlockList.mystery_block);
LOGGER.info("Blocks registered.");
}
}
}
blockstates/mystery_block.json:
{
"variants": {
"": {
"model": "examplemod:block/mystery_block"
}
}
}
models/block/mystery_block.json:
{
"parent": "block/cube_all",
"textures": {
"all": "examplemod:block/mystery_block"
}
}
models/item/mystery_block.json:
{
"parent": "examplemod:block/mystery_block"
}
All that means is that at some point you had a block registered as "examplemod:examplemod", and now you don't. You can safely ignore the message. If you start a new world instead of opening an old one, you won't get that message.
As an aside, HarryTalks is not a recommended way to learn to mod (on the Minecraft Forge forums ). Apparently he uses several bad practices (I have not actually used them).
Alternative suggestions are Cadiboo's example mod, or McJty's tutorials.
I am using hystrix javanica collapser in spring boot, but I found it did not work, my code just like this below:
service class:
public class TestService {
#HystrixCollapser(batchMethod = "getStrList")
public Future<String> getStr(String id) {
System.out.println("single");
return null;
}
#HystrixCommand
public List<String> getStrList(List<String> ids) {
System.out.println("batch,size=" + ids.size());
List<String> strList = Lists.newArrayList();
ids.forEach(id -> strList.add("test"));
return strList;
}
}
where I use:
public static void main(String[] args) {
TestService testService = new TestService();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Future<String> f1= testService.getStr("111");
Future<String> f2= testService.getStr("222");
try {
Thread.sleep(3000);
System.out.println(f1.get()); // nothing printed
System.out.println(f2.get()); // nothing printed
} catch (Exception e) {
}
context.shutdown();
}
It printed 3 single instead of 1 batch.
I want to know what's wrong with my code, a valid example is better.
I can't find a hystrix javanica sample on the internet, So I have to read the source code to solve this problem, now it's solved, and this is my summary:
when you use hystrix(javanica) collapser in spring-boot, you have to:
Defined a hystrixAspect spring bean and import hystrix-strategy.xml;
Annotate single method with #Hystrix Collapser annotate batch method with #HystrixCommand;
Make your single method need 1 parameter(ArgType) return Future , batch method need List return List and make sure size of args be equal to size of return.
Set hystrix properties batchMethod, scope, if you want to collapse requests from multiple user threads, you must set the scope to GLOBAL;
Before you submit a single request, you must init the hystrix context with HystrixRequestContext.initializeContext(), and shutdown the context when your request finish;
I'm working on a Spring 5 project and have some very special expectations with junit. Spring 5 now support junit multithreading and that definitely works very well, I'm now running my hundreds of tests into method parrallel multithreading. But I just setup recently my whole automatic mailing system which works like a charm but that's where it start to be problematic : I run a class that send all my mails to test them, and so they are being sent concurently. But as I just tried right now to test it with not only one email at a time but several, I get a strange SSL handshake error which I related to the fact that concurrent mail sending is not supported by most mail clients.
That's where goes my interrogation: how can I run all my test classes with parallel methods execution except for that email batch sending class?
Maybe I should think about a mail queue to avoid this kind of problem in live? Anyone has an idea?
By the way, in case you wonder I'm yet using gmail client to send mail as I didn't configured it yet for our live mail sending but it will be achieved using dedicated 1and1.fr smtp email client.
Thanks for your patience!
For those who feels interested about the solution, here is how I solved it:
I created a new Singleton class which would handle the queue :
public class EmailQueueHandler {
/** private Constructor */
private EmailQueueHandler() {}
/** Holder */
private static class EmailQueueHandlerHolder
{
/** unique instance non preinitialized */
private final static EmailQueueHandler INSTANCE = new EmailQueueHandler();
}
/** access point for unique instanciation of the singleton **/
public static EmailQueueHandler getInstance()
{
return EmailQueueHandlerHolder.INSTANCE;
}
private List<EmailPreparator> queue = new ArrayList<>();
public void queue(EmailPreparator email) {
waitForQueueHandlerToBeAvailable();
queue.add(email);
}
public List<EmailPreparator> getQueue()
{
waitForQueueHandlerToBeAvailable();
List<EmailPreparator> preparators = queue;
queue = new ArrayList<>();
return preparators;
}
// This method is used to make this handler thread safe
private synchronized void waitForQueueHandlerToBeAvailable(){}
}
I then created a CRON task using #Schedule annotation in my Scheduler bean in which I would correctly handle any mail sending fail.
#Scheduled(fixedRate = 30 * SECOND)
public void sendMailsInQueue()
{
List<EmailPreparator> queue = emailQueueHandler.getQueue();
int mailsSent = queue.size();
int mailsFailed = 0;
for(EmailPreparator preparator : queue)
{
try {
// And finally send the mail
emailSenderService.sendMail(preparator);
}
// If mail sending is not activated, mail sending function will throw an exception,
// Therefore we have to catch it and only throw it back if the email was really supposed to be sent
catch(Exception e)
{
mailsSent --;
// If we are not in test Env
if(!SpringConfiguration.isTestEnv())
{
mailsFailed ++;
preparator.getEmail().setTriggeredExceptionName(e.getMessage()).update();
// This will log the error into the database and eventually
// print it to the console if in LOCAL env
new Error()
.setTriggeredException(e)
.setErrorMessage(e.getClass().getName());
}
else if(SpringConfiguration.SEND_MAIL_ANYWAY_IN_TEST_ENV || preparator.isForceSend())
{
mailsFailed ++;
throw new EmailException(e);
}
}
}
log.info("CRON Task - " + mailsSent + " were successfuly sent ");
if(mailsFailed > 0)
log.warn("CRON Task - But " + mailsFailed + " could not be sent");
}
And then I called this mail queue emptyer methods at the end of each unit test in my #After annotated method to make sure it's called before I unit test the mail resulted. This way I'm aware of any mail sending fail even if it appear in PROD env and I'm also aware of any mail content creation failure when testing.
#After
public void downUp() throws Exception
{
proceedMailQueueManuallyIfNotAlreadySent();
logger.debug("After Test");
RequestHolder requestHolder = securityContextBuilder.getSecurityContextHolder().getRequestHolder();
// We check mails sending if some were sent
if(requestHolder.isExtResultsSent())
{
for(ExtResults results : requestHolder.getExtResults())
{
ExtSenderVerificationResolver resolver =
new ExtSenderVerificationResolver(
results,
notificationParserService
);
resolver.assertExtSending();
}
}
// Some code
}
protected void proceedMailQueueManuallyIfNotAlreadySent()
{
if(!mailQueueProceeded)
{
mailQueueProceeded = true;
scheduler.sendMailsInQueue();
}
}
I have a service called TestService which extends AbstractVerticle:
public class TestService extends AbstractVerticle {
#Override
public void start() throws Exception {
//Do things
}
}
I then deploy that verticle with vertx like this:
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(TestService.class.getName());
How can I get a reference to my deployed TestService after vertx instantiates it?
You should use an alternative method for deployment:
vertx.deployVerticle(TestService.class.getName(), deployment -> {
if (deployment.succeeded()) {
// here is your ID
String deploymentId = deployment.result();
} else {
// deployment failed...
}
});
If you're just interested in listing all deployed verticles then you can just request the list of ids:
vertx.deploymentIDs()
I know this question is old however it may be useful to someone to see an example of how to do this.
You will often see examples for deployment like this from vertx-examples
this follows as asynchronous micro service framework, however its really easy to get the reference as the method 'deployVerticle' (see line 29 in the link) will take an instance as shown in the simple example below, and u can get a reference in the call back as shown.
example in Kotlin easily translate to java
MyVert: io.vertx.core.AbstractVerticle() {
override fun start() {
// init
}
fun someFunction() {
}
}
fun main() {
val vertx = Vertx.vertx()
val myVert = MyVert()
vertx.deployVerticle(myVert) {
if(it.succeeded() ) {
myVert.someFunction()
}
else { println(it.cause().localizedMessage)} }
}
you can get all deployed verticles in current vertx instance by this way
Set<String> strings = vertx.deploymentIDs();
strings
.stream()
.map(id -> ((VertxImpl)vertx.getDelegate()).getDeployment(id))
.forEach(deployment -> System.out.println(deployment.verticleIdentifier() + " " + deployment.isChild() ));
Looks like the vertx API does not allow you to retrieve the Verticle objects once they are deployed. Maybe because verticles can be distributed over multiple JVM.
I needed to do it for unit tests though and I came up with this.
This is unreliable since you rely on VertxImpl (it can break at any vertx version upgrade). But I prefer this over changing production code to be able to test it.
private static <T extends Verticle> List<T> retrieveVerticles(Vertx vertx, Class<T> verticleClass) {
VertxImpl vertxImpl = (VertxImpl) vertx;
return vertxImpl.deploymentIDs().stream().
map(vertxImpl::getDeployment).
map(Deployment::getVerticles).
flatMap(Set::stream).
filter(verticleClass::isInstance).
map(verticleClass::cast).
collect(Collectors.toList());
}
Usage example:
vertx.deployVerticle(new MainVerticle());
// some MyCustomVerticle instances are deployed from the MainVerticle.start
// you can't reach the MyCustomVerticle objects from there
// so the trick is to rely on VertxImpl
List<MyCustomVerticle> deployedVerticles = retrieveVerticles(vertx, MyCustomVerticle.class);
I am new to Akka and trying to write some code in Play Framework 2 in Java and use Akka.
To create an actor and send a test message to it, I have:
public class Global extends GlobalSettings {
#Override
public void onStart(Application app) {
final ActorRef testActor = Akka.system().actorOf(Props.create(TestActor.class), "testActor");
testActor.tell("hi", ActorRef.noSender());
}
}
This work perfectly fine and I see that my actor received my message, here is the code for my actor:
public class TestActor extends UntypedActor {
#Override
public void onReceive(Object message) throws Exception {
if(message.toString().equals("hi")){
System.out.println("I received a HI");
}else{
unhandled(message);
}
}
}
Very simple.
However, If I try to send a message from a controller:
public static Result index() {
final ActorRef testActor = Akka.system().actorFor("testActor");
testActor.tell("hi", ActorRef.noSender());
return ok(index.render("Your new application is ready."));
}
I get this message on terminal:
[INFO] [09/20/2014 11:40:30.850] [application-akka.actor.default-dispatcher-4] [akka://application/testActor] Message [java.lang.String] from Actor[akka://application/deadLetters] to Actor[akka://application/testActor] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
Can someone help me with this? why the first usage works and the second one fails? Thanks
The actorFor method requires the entire path, and your actor lives in the user space, so you have to use actorFor("/user/testActor"). Currently you are sending it to application/testActor, which would be a top-level actor in the ActorSystem itself.
By the way, actorFor is deprecated (at least in the Scala API) and replaced by actorSelection.
For more information, refer to the excellent documentation.
actorFor should get the path to the actor which is probably "akka://System/user/testActor". It also does not create the actor, meaning it should be exist.
Anyway, is there a reason that inside the controller you use the actorFor and not the actorOf? It had been deprecated and shouldn't be use.