#Make Spring Boot app run that is not designed for Web

17 messages · Page 1 of 1 (latest)

stuck sun
#

I know that might sound like a stupid question but I have a Spring app which is designed to communicate with a queue instance via cloudamqp (rabbitmq). Basically it is a sensor simulator and I want to get the data from a csv and publish it into my queue. (this app is the producer).
I would like to execute a method from a class and I don't know how to do this. (I guess one way would be to callthat method in my main function), but is this good practice? Usually in Spring Boot apps you don't write anything in your main function and you just let it be like that:

public static void main(String[] args) {
        SpringApplication.run(Producer1Application.class, args);
    }```
The code I would like to execute when i run the Spring app is this:
```java
@Service
@Configuration
public class RabbitMQSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private Queue queue;
    @Autowired
    Parser parser;

    public void send(MeasurementMQ measurementMQ) throws InterruptedException {
        List<MeasurementMQ> measurementMQS = parser.readFromCSV();
        for(MeasurementMQ measurement: measurementMQS){
            rabbitTemplate.convertAndSend(queue.getName(), measurement);
            System.out.println("Sending message to queue: " + measurementMQ.toString());
            Thread.sleep(1000);
        }
    }```
(more specifically the `send` method).
I don't have experience with these types of apps (not-web), because everything that I did using Spring was for Web, and, so I had some Tomcat server running on 8080 which solved this problem. Would appreciate some help. Thanks!
leaden pollenBOT
#

This post has been reserved for your question.

Hey @stuck sun! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

terse hill
stuck sun
#

yes

terse hill
#

I assume it will read a file, parse csv, call this sender

stuck sun
#
@Component
public class Parser {
    private final Scanner sc;

    public Parser() throws FileNotFoundException {
        sc = new Scanner(new File("src/main/resources/sensor_data1.csv"));
    }

    public List<MeasurementMQ> readFromCSV() {
        List<MeasurementMQ> measurementMQS = new ArrayList<MeasurementMQ>();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            MeasurementMQ measurementMQ = new MeasurementMQ(LocalDateTime.now().toLocalTime().toString(), "24", line);
            measurementMQS.add(measurementMQ);
        }

        return measurementMQS;
    }
}
terse hill
#

Right

#

So you can have a postconstruct annotated method here which calls readfromcsv

#

Or indeed annotate readfromcsv itself with postconstruct

stuck sun
#

so this postconstruct means that after the @Autowired the method gets called instantly?

#

because this Parser is Autowired in my RabbitMQSender

#

I found 1more solution I think using the CommandLineRunner interface in the main class

terse hill
stuck sun
#

thanks

leaden pollenBOT
# leaden pollen

Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.