我需要session manager来连接SAP服务器。我使用spring bean来创建session manager。我从hibersap.xml
获取SAP服务器配置。我以前连接到单个服务器。但现在我需要连接到两个或更多服务器。为了从hibersap.xml
读取必要的配置,我需要在运行时更新spring bean,我该怎么做?
2个独立的会话管理器,名称分别为BHD和AHD。
<session-manager name="BHD">
<context>org.hibersap.execution.jco.JCoContext</context>
<properties>
<property name="jco.client.client" value="111" />
<property name="jco.client.user" value="AAA" />
<property name="jco.client.passwd" value="11Q1Q1Q1Q" />
<property name="jco.client.ashost" value="11.11.11.11" />
<property name="jco.client.sysnr" value="00" />
</properties>
<annotated-classes>
<annotated-class>com.ldap.connection.sap.bapi.RfcLdapParameters</annotated-class>
<annotated-class>com.ldap.connection.sap.bapi.RfcSapUserDetails</annotated-class>
<annotated-class>com.ldap.connection.sap.bapi.UsernameAndMailModel</annotated-class>
</annotated-classes>
</session-manager>
<session-manager name="AHD">
<context>org.hibersap.execution.jco.JCoContext</context>
<properties>
<property name="jco.client.client" value="222" />
<property name="jco.client.user" value="BBB" />
<property name="jco.client.passwd" value="2Q2Q2Q2Q22" />
<property name="jco.client.ashost" value="22.22.22.22" />
<property name="jco.client.sysnr" value="00" />
</properties>
<annotated-classes>
<annotated-class>com.ldap.connection.sap.bapi.RfcLdapParameters</annotated-class>
<annotated-class>com.ldap.connection.sap.bapi.RfcSapUserDetails</annotated-class>
<annotated-class>com.ldap.connection.sap.bapi.UsernameAndMailModel</annotated-class>
</annotated-classes>
</session-manager>
将下面这一行(-〉configuration = new AnnotationConfiguration("BHD")
)中的BHD值更新为AHD可以解决这个问题。但是我如何在运行时做到这一点呢?
@Configuration
@Slf4j
public class HibersapConfiguration {
@Bean
public SessionManager sessionManager() {
String sname = System.getProperty("java.systemname");
AnnotationConfiguration configuration = null;
if(sname != null) {
if(BtcConstants.LIVE_SYSTEM_NAME.equals(sname)) {
log.info("SAP üretim canlı sisteme bağlanılıyor...");
}
if(BtcConstants.DEV_SYSTEM_NAME.equals(sname)){
configuration = new AnnotationConfiguration("BHD");
log.info("SAP üretim dev sisteme bağlanılıyor...");
}
}else {
configuration = new AnnotationConfiguration("BHD");
log.info("Local'den SAP üretim dev sisteme bağlanılıyor...");
}
return configuration.buildSessionManager();
} }
我注入会话管理器的存储库:
@Slf4j
@Repository
public class SapUserRepository implements SapUserRepositoryI {
private final SessionManager sessionManager;
public SapUserRepository(SessionManager sessionManager) {
this.sessionManager = sessionManager;
}
@Override
public List<SapUser> findPersonalDetailFromSap(Date currentDate, String pernr) {
List<SapUser> sapUserList = new ArrayList<SapUser>();
RfcSapUserDetails details = new RfcSapUserDetails();
details.setCurrentDate(currentDate);
details.setPernr(pernr);
Session session = null;
if(sessionManager != null) {
log.info("'findPersonalDetailFromSap' için session açıldı");
session = sessionManager.openSession();
try {
log.info("Personel verileri çekiliyor...");
session.execute(details);
sapUserList = details.getSapUserList();
} finally {
session.close();
log.info("session kapatıldı");
}
}else {
log.error("sessionManager null geldi !");
}
return sapUserList;
}
1条答案
按热度按时间vq8itlhq1#
我不是SAP技术方面的Maven,但您可能希望创建2个单独的
SessionManager
并按名称(@Qualifier
)注入它们。Spring如何知道你想连接到你的仓库中的哪个服务器?你必须自己告诉Spring。上面的代码就是这样的一种方式。
现在,如果您想让AHD服务器作为后备(如果连接到BHD失败,则连接到AHD),您可以创建一个很好的 Package 器,如