Я не могу получить доступ к методу getCpuUsage () из моего кода x html через
<h:outputText id="cpu_usage"
value="#{cpuUsageBean.cpuUsage} %" />
Когда я добавляю аннотацию @bean к методу getCpuUsage (), этот метод работает, когда компонент инициализирован. Но после что, когда я пытаюсь получить доступ через файл x html, увиденный на странице, ничего не происходит вообще. Надеюсь, вы меня хорошо понимаете. Заранее спасибо
my x html:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body style="margin-left:50px">
<h2>PrimeFaces Polling Example</h2>
<h:form>
CPU usage:
<b> <h:outputText id="cpu_usage"
value="#{cpuUsageBean.cpuUsage} %" />
</b>
<p:poll interval="1" update="cpu_usage" />
</h:form>
</h:body>
</html>
МОЙ УПРАВЛЯЕМЫЙ БИН ТАК КОМПОНЕНТ:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.InitBinder;
import lombok.extern.slf4j.Slf4j;
@Component
@Scope("VIEW")
//@ViewScoped
//@Named
@Slf4j
public class CpuUsageBean {
private AtomicInteger cpuUsage;
@PostConstruct
public void init() {
cpuUsage = new AtomicInteger(50);
ExecutorService es = Executors.newFixedThreadPool(1);
es.execute(() -> {
while (true) {
// simulating cpu usage
int i = ThreadLocalRandom.current().nextInt(-10, 11);
int usage = cpuUsage.get();
usage += i;
if (usage < 0) {
usage = 0;
} else if (usage > 100) {
usage = 100;
}
cpuUsage.set(usage);
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
}
}
});
}
public int getCpuUsage() {
System.out.println("-----getCpuUsage : " + cpuUsage);
log.error("---------- getCpuUsage calisti");
return cpuUsage.get();
}
}
МОЙ SPRİNGBOOT APP CLASS:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.tutorial.PrimeFacesTutorial.controller,com.tutorial.PrimeFacesTutorial")
public class PrimeFacesTutorialApplication {
public static void main(String[] args) {
SpringApplication.run(PrimeFacesTutorialApplication.class, args);
}
}