001package ball.spring.mysqld;
002/*-
003 * ##########################################################################
004 * Reusable Spring Components
005 * $Id: MysqldComponent.html 5431 2020-02-12 19:03:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-10-19-spring-embedded-mysqld/src/main/resources/javadoc/src-html/ball/spring/mysqld/MysqldComponent.html $
007 * %%
008 * Copyright (C) 2018 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import lombok.NoArgsConstructor;
024import lombok.ToString;
025import lombok.extern.log4j.Log4j2;
026import org.springframework.beans.factory.annotation.Autowired;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
028import org.springframework.scheduling.annotation.Scheduled;
029import org.springframework.stereotype.Component;
030
031import static java.util.concurrent.TimeUnit.SECONDS;
032
033/**
034 * {@code mysqld} {@link Component}.
035 *
036 * {@injected.fields}
037 *
038 * @see MysqldConfiguration
039 *
040 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
041 * @version $Revision: 5431 $
042 */
043@Component
044@ConditionalOnBean(name = { "mysqld" })
045@NoArgsConstructor @ToString @Log4j2
046public class MysqldComponent {
047    @Autowired private Process mysqld;
048
049    @Scheduled(fixedRate = 15 * 1000)
050    public void run() {
051        if (mysqld != null) {
052            if (mysqld.isAlive()) {
053                try {
054                    mysqld.waitFor(15, SECONDS);
055                } catch (InterruptedException exception) {
056                }
057            } else {
058                throw new IllegalStateException("mysqld is not running");
059            }
060        }
061    }
062}